I’m looking for some advice on how to run Nix on my Linux machine.
I see a lot of things related to Nix: nix profile, nix-env, flakes,…, but honestly I’m lost :).
My goal is to install, configure and maintain software with Nix on my Linux machine.
With that mind, what is the best approach of using nix on Linux?
Do you want to use NixOS or nix the package manager? Either way, the first step is installing either one, for which the instructions are pretty clear and not very difficult.
I’m assuming you mean non-NixOS Linux distros. If you just want to use Linux, simply install NixOS and ignore the rest of this - NixOS is a Linux distribution.
If you do mean another distro, you say:
Assuming that by “maintain” you just mean maintaining your configuration, sounds like what you’re looking for is declarative configuration + installation on non-NixOS Linux distros.
In theory this can be accomplished a couple of ways, since it ultimately just boils down to writing a deployment script with nix that works on the distro in question.
In practice, the way to do this is home-manager, which gives you a module system similar to the one that makes up NixOS. On non-NixOS distros you specifically need one of the ways it can be used “standalone”.
Only problem with this is that home-manager is restricted to your user.
If you want to install and configure software system-wide on non-NixOS distros, rather than just for a user, there isn’t all that much out there at the moment. There are some initial ideas around how NixOS modules could be abstracted to work on other distros, and there is at least a WIP project that provides something like this, but there isn’t really a good way to accomplish this goal in general right now.
You can use nix-built software on other distros, but deploying configuration is a significantly harder problem to solve, since nix cannot be in full control.
I’m not moving to NixOS yet, I just want to use nix the package manager.
It’s on a working station where I want to install packages that are not provided by my distribution.
I did a try with nix profiles and flakes but I’m not sure I’m going into the right direction.
I started to test with some basic packages
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs";
};
outputs = { self, nixpkgs }: {
packages."x86_64-linux".default = let
pkgs = nixpkgs.legacyPackages."x86_64-linux";
in pkgs.buildEnv {
name = "home-packages";
paths = with pkgs; [
git
zsh
];
};
};
}
Ah, I see, yes, that is an option too, if all you care about is installing a few binaries. You can write the outputs a bit more cleanly by factoring the system into a variable:
outputs = { self, nixpkgs }: let
system = "x86_64-linux";
in {
packages.${system}.default = let
pkgs = nixpkgs.legacyPackages."x86_64-linux";
in pkgs.buildEnv {
name = "home-packages";
paths = with pkgs; [
git
zsh
];
};
};
This will definitely work, but it has a major problem: if you make a change to the profile after you have installed it, and then use nix profile installagain, now you’ve installed this profile twice.
This means that e.g. if you remove a package from the list, and then use install, the package will still be installed.
To not have this behavior, you need to use nix profile upgrade '.*'. Undoing a mistaken nix profile install is possible too, but a bit tricky.
Personally I think I would still recommend using home-manager, since it sorts out a lot of orthogonal details for you. There are a bunch of things you may not realize are missing when just using a nix profile (e.g. completion and man pages) which home-manager’s rcfile configures correctly.
Yes, though I’d suggest you follow their docs and use the init command to help you here: Home Manager Manual
Yes, you would use the home-manager command instead of using nix profile. The home-manager command secretly also calls nix profile internally, and calls some scripts to create files and start systemd units (if you activate any of that in your home-manager config).