Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to keep my Nix packages in sync across machines not running NixOS?

Tags:

nix

I know with NixOS, you can simply copy over the configuration.nix file to sync your OS state including installed packages between machines.

Is it possible then, to do the same using Nix the package manager on a non-NixOS OS to sync only the installed packages?

like image 993
Lewis Avatar asked Jan 05 '23 16:01

Lewis


1 Answers

Please note, that at least since 30.03.2017 (corresponding to 17.03 Nix/NixOS channel/release), as far as I understand the official, modern, supported and suggested solution is to use the so called overlays.

See the chapter titled "Overlays" in the nixpkgs manual for a nice guide on how to use the new approach.

As a short summary: you can put any number of files with .nix extension in $HOME/.config/nixpkgs/overlays/ directory. They will be processed in alphabetical order, and each one can modify the set of available Nix packages. Each of the files must be written as in the following pattern:

self: super:
{
  boost = super.boost.override {
    python = self.python3;
  };
  rr = super.callPackage ./pkgs/rr {
    stdenv = self.stdenv_32bit;
  };
}
  • The super set corresponds to the "old" set of packages (before the overlay was applied). If you want to refer to the old version of a package (as in boost above), or callPackage, you should reference it via super.

  • The self set corresponds to the eventual, "future" set of packages, representing the final result after all overlays are applied. (Note: don't be scared when sometimes using them might get rejected by Nix, as it would result in infinite recursion. Probably you should rather just use super in those cases instead.)

Note: with the above changes, the solution I mention below in the original answer seems "deprecated" now — I believe it should still work as of April 2017, but I have no idea for how long. It appears marked as "obsolete" in the nixpkgs repository.



Old answer, before 17.03:

Assuming you want to synchronize apps per-user (as non-NixOS Nix keeps apps visible on per-user basis, not system-wide, as far as I know), it is possible to do it declaratively. It's just not well advertised in the manual — though it seems quite popular among long-time Nixers!

You must create a text file at: $HOME/.nixpkgs/config.nix — e.g.:

$ mkdir -p ~/.nixpkgs
$ $EDITOR ~/.nixpkgs/config.nix

then enter the following contents:

{
  packageOverrides = defaultPkgs: with defaultPkgs; {
    home = with pkgs; buildEnv {
      name = "home";
      paths = [
        nethack mc pstree #...your favourite pkgs here...
      ];
    };
  };
}

Then you should be able to install all listed packages with:

$ nix-env -i home
or:
$ nix-env -iA nixos.home   # *much* faster than above

In paths you can put stuff in a similar way like in /etc/nixos/configuration.nix on NixOS. Also, home is actually a "fake package" here. You can add more custom package definitions beside it, and then include them your "paths".

(Side note: I'm hoping to write a blog post with what I learned on how exactly this works, and also showing how to extend it with more customizations. I'll try to remember to link it here if I succeed.)

like image 187
akavel Avatar answered May 16 '23 07:05

akavel