Migrating NixOS configuration.nix inputs from nix-channel to npins and system.nix

Introduction

From the 26.05 release notes:

The system.nix file has been added as an alternative entry point to configuration.nix (and flake.nix) that allows configuring NixOS without using nix-channel.

Motivated by this thread, which the source of most of what I wrote here, I’ve switched one of my machines from channels to npins and I wrote down the steps for myself. After a few days of updates without issues, I have applied it to another machine successfully and thought it could be useful to others.

My starting point

  • A git repository with one directory per machine, let’s say laptop+desktop. The repo is checked out as root in /etc/nixos and I have a symlink to /etc/nixos/laptop/configuration.nix. Certainly not the most elegant or sophisticated setup, but it has worked for me for a few years now.
  • home-manager imported as a nixos module (aka <home-manager/nixos>)
β”œβ”€β”€ common
β”‚   β”œβ”€β”€ git.nix
β”‚   └── neovim.nix
β”œβ”€β”€ configuration.nix -> desktop/configuration.nix
β”œβ”€β”€ laptop
β”‚   └── configuration.nix
β”œβ”€β”€ desktop
β”‚   └── configuration.nix
└── hardware-configuration.nix

Migration steps

  1. Add npins to systemPackages, ensure the system is up-to-date, find out exactly which channels are in use via nix-channel --list.
  2. Initialize npins in the machine’s subdirectory, not globally in the repo since each machine will eventually have its own npins: npins init --bare then npins add --name nixpkgs channel nixos-26.05.
  3. Create a /etc/nixos/desktop/system.nix file as explained in the release notes but using npins:
let
  sources = import ./npins;
in
import "${sources.nixpkgs}/nixos" {
  configuration = ./configuration.nix;
}

Then add a new symlink to it in /etc/nixos/, run nixos-rebuild switch to confirm that it works so far, it should result in the same system as in step 1.

  1. Add home-manager: npins add --name home-manager github nix-community home-manager -b release-26.05
    and change configuration.nix to import npins and use it for home-manager:
let
  sources = import ./npins;
in
{
  imports = [
    /etc/nixos/hardware-configuration.nix
    "${sources.home-manager}/nixos"
  ];
(...)
  1. Add the nixPath setup and a symlink in configuration.nix as recommended by @rnhmjoj (thank you!)
nix = {
 nixPath = [
   "nixos-config=/etc/nixos/configuration.nix"
   "nixos-system=/etc/nixos/system.nix"
   "nixpkgs=/run/current-system/nixpkgs"
 ];
};
system.systemBuilderCommands = "ln -s ${sources.nixpkgs} $out/nixpkgs";
  1. Do a nixos-rebuild switch and re-open your shell to ensure $NIX_PATH is the new one.
  2. Clear the channels nix-channel --remove home-manager, nix-channel --remove nixos
  3. Disable channels by adding nix.channel.enable = false; to configuration.nix and do another rebuild.
  4. Remove the channel remnants found by nixos-rebuild, if any. For me that meant rm /nix/var/nix/profiles/per-user/root/channels which just contained an empty manifest.nix after previous step.

And that’s it!
The diff on the repository is really small, configuration.nix: 6 new and 1 modifiedlines.
Then there’s a new 6 lines long system.nix and the npins stuff.

After that

  • nixos-rebuild switch --upgrade becomes cd /etc/nixos/desktop && npins update && nixos-rebuild switch
  • nixos-rebuild switch still works as before

Result

β”œβ”€β”€ common
β”‚   β”œβ”€β”€ git.nix
β”‚   └── neovim.nix
β”œβ”€β”€ configuration.nix -> desktop/configuration.nix
β”œβ”€β”€ laptop
β”‚   └── configuration.nix
β”œβ”€β”€ desktop
β”‚   β”œβ”€β”€ configuration.nix
β”‚   β”œβ”€β”€ npins
β”‚   β”‚   β”œβ”€β”€ default.nix
β”‚   β”‚   └── sources.json
β”‚   └── system.nix
β”œβ”€β”€ hardware-configuration.nix
└── system.nix -> desktop/system.nix
9 Likes

Thank you for sharing! I don’t want to be that guy that gets something for free and asks for more, but if you feel like it, I think such migration documentation will be welcome in the NixOS manual. Or, some precursor formalisation work. Well, thanks again anyway.

I initially thought about putting it on the wiki but I would find it strange to see it in anything official since it relies so much on npins, a thirdparty tool, not even under the nix-community org.

If we can find a good spot for it, I’d be willing to reshape it.

Putting it on the wiki is fine, it’s just one solution, and the wiki is not official docs.

3 Likes

Well, perhaps a basic amount of reference level documentation in NixOS about this. I think that this was implemented in nixos-rebuid and never documented outside of it. So, perhaps the NixOS manual?

3 Likes