5

I am only using Nix as a package manager and not using all of NixOS. I would like a reproducible nix-env -i package installation which can be shared and backed up.

I am aware of using config.nix for for NixOS but I am looking for similar functionality with just Nix packages.

hackeryarn
  • 302
  • 3
  • 10

2 Answers2

5

From Nixpkgs you can use the buildEnv function to construct symlink farms similar to how nix-env produces them.

This lets you group packages together into groups that you want to update separately. Of course a single group is perfectly valid if that suits your applications.

Here's an example greeting-tools.nix:

let
  pkgs = import <nixpkgs> {};
  inherit (pkgs) buildEnv;

in buildEnv {
  name = "greeting-tools";
  paths = [ pkgs.hello pkgs.cowsay pkgs.figlet ];
}

You can install it and remove it as follows

$ nix-env -i -f greeting-tools.nix
installing 'greeting-tools'
$ hello
Hello, world!
$ nix-env -e greeting-tools
uninstalling 'greeting-tools'
$ hello
The program ‘hello’ is currently not installed. [...]

To update your packages, you have to re-run the installation command. nix-env -u will not work correctly because that only looks at Nixpkgs, which probably doesn't have anything named like that.

An alternative may be home manager.

Robert Hensing
  • 6,708
  • 18
  • 23
  • Thank you, this is perfect and much simpler than other solutions I have seen! I am using nix as my global package manager at the moment so I can definitely utilze this and just backup my install file to github. – hackeryarn Jun 11 '18 at 21:43
-1

I'm still new, but discovered https://nixos.org/manual/nixpkgs/stable/#sec-building-environment which should do exactly the same and what was being asked.

E.g. inside ~/.config/nixpkgs/config.nix

{
  packageOverrides = pkgs: with pkgs; {
    myPackages = pkgs.buildEnv {
      name = "greeting-tools";
      paths = [
        hello
        cowsay
        figlet
      ];
    };
  };
}

And install via nix-env -iA nixpkgs.myPackages

Another way would be to define a new overlay, e.g. in ~/.config/nixpkgs/overlays/greeting-tools/default.nix:

self: super: {
  greetingTools = super.buildEnv {
    name = "greeting-tools";
    paths = [
      self.hello
      self.cowsay
      self.figlet
    ];
  };
}

Which then can be installed like any other package: nix-env -i my-packages

Daniel
  • 940
  • 6
  • 12