How to improve nixos-rebuild switch speed?

Running sudo nixos-rebuild switch takes around 15 seconds. Using flakes and home manager.

Wondering how to reduce the time and if you all have workflows to work around this? Especially for home manager where fast iteration times are important.

My flake.nix:

{
  description = "NixOS System";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable/";
    home-manager.url = "github:nix-community/home-manager";
    home-manager.inputs.nixpkgs.follows = "nixpkgs";
    stylix.url = "github:danth/stylix";

    nixos-cosmic = {
      url = "github:lilyinstarlight/nixos-cosmic";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = {
    nixpkgs,
    stylix,
    ...
  } @ inputs: {
    nixosConfigurations.nixos = nixpkgs.lib.nixosSystem {
      specialArgs = {inherit inputs;};
      system = "x86_64-linux";
      modules = [
        stylix.nixosModules.stylix
        inputs.home-manager.nixosModules.home-manager
        {
          home-manager.useGlobalPkgs = true;
          home-manager.useUserPackages = true;
          home-manager.backupFileExtension = "backup";
          home-manager.users.tarik = import ./home.nix;
        }
        {
          nix.settings = {
            substituters = ["https://cosmic.cachix.org/"];
            trusted-public-keys = ["cosmic.cachix.org-1:Dya9IyXD4xdBehWjrkPv6rtxpmMdRel02smYzA85dPE="];
          };
        }
        inputs.nixos-cosmic.nixosModules.default
        ./configuration.nix
      ];
    };
  };
}

Thank you!

1 Like

15 seconds is fast unfortunately. From experience, HM causes massive slowdowns compared to just NixOS by itself, rebuilds (without any nixpkgs updates) went from 30s to 10s in my config when I stopped using HM.

3 Likes

Yep. My activation times often exceed one and a half minutes, mostly due to the systemd-boot builder and services. 15 seconds is amazing.

1 Like

Using lix will halve the eval time

5 Likes

If speed is all you care about:

  1. Don’t use flakes because they copy your entire local config repo to the nix store with every single eval.
  2. Don’t integrate home-manager into your NixOS. It’s another module system eval of a similarly sized module system as NixOS and therefore takes similarly long to eval.
  3. Don’t use specialisations or NixOS containers. They cause an entire NixOS module eval for each container or specialisation.
  4. Minimise binary caches or don’t use any other than cache.nixos.org. Each one has to be queried for each output path/derivation that is not already present before Nix starts to build the derivation locally.
5 Likes