I use flakes for managing my homeserver that’s running NixOS. The non-flake version of Nix uses channels, and any nix shell created on a system that doesn’t use flakes also seems to inherit package versions from whatever channel the system is using. For my flake-based setup, I’m using the unstable channel to import nixpkgs. However, the nix shell appears to use some really old (stable?) channel of nixpkgs, giving me really old package versions if I need to spin up an ad-hoc shell for something.

I found this rather useful comment on reddit that describes how to set up an ad-hoc shell that uses the latest unstable version of nixpkgs instead. From the comment, here’s the content of a script.nix:

let
  unstableTarball =
    fetchTarball
      https://github.com/NixOS/nixpkgs-channels/archive/nixos-unstable.tar.gz;
  pkgs = import <nixpkgs> {};
  unstable = import unstableTarball {};

  shell = pkgs.mkShell {
    buildInputs = [ pkgs.gptfdisk unstable.peep ];
  };
in shell

and a nix shell can be opened using this script by running nix-shell script.nix. In the example above, the shell would contain the gptfdisk version from your system’s nixpkgs and the peep version from the latest unstable nixpkgs.

Tagged: