Search code examples
nix

nix copy a nixpkgs with overlays definition


Is there a way to recover the package set created by nixpkgs with overlays in a way that is compatible with nix copy?

Take for instance the following statements in nix repl

overlays = [(self: super: {aardvark = "does aardvark";})]
pkgs = import <nixpkgs> { inherit overlays; }
pkgs2 = import pkgs.path {}
pkgs3 = import pkgs.path { inherit (pkgs) overlays; }

pkgs.aardvark exists and was put there by the overlay, but if I try to reimport the resulting pkgs path, I only import the store version of <nixpkgs>.

pkgs3.aardvark also exists but this is not the solution I am looking for.

My intended use is to perform a nix copy of the pkgs with the overlays. I can copy the pkgs.path definition, but not the overlays since its a list of lambda's, not paths. I'm hoping for a really easy solution, like instead of pkgs.path there's an attribute like pkgs.drv or pkgs.out that can be imported with e.g. import pkgs.out and delivers the overridden package set.


Solution

  • Overlays exist only at the Nix language level. They're just a pattern of functions attribute sets. The implementation of the Nix language does not provide the operations to serialize arbitrary expressions and copy those. So if you want to copy Nix expressions, you'll have to do it yourself at the file level.

    Taking a step back, perhaps it's better to either ship built closures or share a git repo. Shipping built closures is what NixOps does and has the advantage that everything can be defined in one Nix evaluation process, but doesn't allow users of the machine to work with the expressions that defined the machine. This is a good tradeoff for push-deployed servers. Similarly, you can perform a remote deployment without NixOps using nixos-rebuild --target-host or something custom based on nix-copy-closure.

    If on the other hand you want to initiate the installation from remote machine, you'll need to have the expressions there and a git repo is probably the most practical way to achieve that.