Unable to nixos-rebuild switch (with flake)

I am trying out nix flakes for my /etc/nixos/configuration.nix, and now I’m unable to do nixos-rebuild switch without errors.

I get:

╭─hhefesto@Olimpo ~ 
╰─$ sudo nixos-rebuild switch --flake '/etc/nixos#olimpo'                        
warning: Git tree '/etc/nixos' is dirty
building the system configuration...
warning: Git tree '/etc/nixos' is dirty
Traceback (most recent call last):
  File "/nix/store/6dmc2579q6dmmx0dkv4wc243q89n5mxq-systemd-boot-builder.py", line 256, in <module>
    main()
  File "/nix/store/6dmc2579q6dmmx0dkv4wc243q89n5mxq-systemd-boot-builder.py", line 223, in main
    remove_old_entries(gens)
  File "/nix/store/6dmc2579q6dmmx0dkv4wc243q89n5mxq-systemd-boot-builder.py", line 145, in remove_old_entries
    known_paths.append(copy_from_profile(*gen, "kernel", True))
  File "/nix/store/6dmc2579q6dmmx0dkv4wc243q89n5mxq-systemd-boot-builder.py", line 62, in copy_from_profile
    store_file_path = profile_path(profile, generation, name)
  File "/nix/store/6dmc2579q6dmmx0dkv4wc243q89n5mxq-systemd-boot-builder.py", line 59, in profile_path
    return os.readlink("%s/%s" % (system_dir(profile, generation), name))
FileNotFoundError: [Errno 2] No such file or directory: '/nix/var/nix/profiles/system-6-link/kernel'
warning: error(s) occurred while switching to the new configuration

From /nix/var/nix/profiles/system-1-link to /nix/var/nix/profiles/system-5-link the kernel file does exist. From /nix/var/nix/profiles/system-6-link to /nix/var/nix/profiles/system-20-link it’s not there (I guess system-6-link was my first configuration with flake).

This is my flake:

{
  description = "hhefesto's system configuration";

  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

  outputs = inputs@{ self, nixpkgs, ... }:
  {
    nixosConfigurations.olimpo = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [ (import ./configuration.nix) ];
      specialArgs = { inherit inputs; };
    };

  };
}

partially trimed configuration.nix :

{ config, pkgs, lib, modulesPath, inputs, ... }:
{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  networking.hostName = "olimpo"; # Define your hostname.

  nixpkgs.config.allowUnfree = true;

  environment.systemPackages = with pkgs; [
    emacs
    git
  ];


  programs.light.enable = true;

  programs.zsh = {
    enable = true;
    enableCompletion = true;
    autosuggestions.enable = true;
    interactiveShellInit = ''
      # z - jump around
      # source ${pkgs.fetchurl {url = "https://github.com/rupa/z/raw/2ebe419ae18316c5597dd5fb84b5d8595ff1dde9/z.sh"; sha256 = "0ywpgk3ksjq7g30bqbhl9znz3jh6jfg8lxnbdbaiipzgsy41vi10";}}
      save_aliases=$(alias -L)
      export ZSH=${pkgs.oh-my-zsh}/share/oh-my-zsh
      export ZSH_THEME="bira" #"lambda"
      plugins=(git sudo colorize extract history postgres)
      source $ZSH/oh-my-zsh.sh
      eval $save_aliases; unset save_aliases
    '';
    promptInit = ''
      any-nix-shell zsh --info-right | source /dev/stdin
  '';
  };

  networking.firewall.allowedTCPPorts = [ 3000 5432 587 5938 ];
  networking.firewall.allowedUDPPorts = [ 5938 ];

  services.openssh.enable = true;

  services.sshd.enable = true;

  services.xserver.enable = true;
  services.xserver.layout = "us";
  services.xserver.xkbOptions = "ctrl:nocaps";
  services.xserver.xkbVariant = "altgr-intl";
  services.xserver.windowManager.xmonad = {
    enable = true;
    enableContribAndExtras = true;
    extraPackages = haskellPackages:[
      haskellPackages.xmonad-contrib
      haskellPackages.xmonad-extras
      haskellPackages.xmonad
    ];
  };
  services.xserver.displayManager = {
    defaultSession = "gnome";
    gdm.enable = true;
    # autoLogin.user = "hhefesto";
  };
  services.xserver.desktopManager.gnome3.enable = true;

  virtualisation.docker.enable = true;
  virtualisation.virtualbox.host.enable = true;


  users.mutableUsers = false;

  users.users.root.initialHashedPassword = "$6$/RvS0Se.iCx$A0eA/8PzgMj.Ms9ohNamfu53c9S.zdG30hEmUHLjmWP0CaXTPVA6QxGIZ6fy.abkjSOTJMAq7fFL6LUBGs4BU0";
  users.users.hhefesto.initialHashedPassword = "$6$/RvS0Se.iCx$A0eA/8PzgMj.Ms9ohNamfu53c9S.zdG30hEmUHLjmWP0CaXTPVA6QxGIZ6fy.abkjSOTJMAq7fFL6LUBGs4BU0"; # this may be redundant

  users.extraUsers.hhefesto = {
    createHome = true;
    isNormalUser = true;
    home = "/home/hhefesto";
    description = "Daniel Herrera";
    extraGroups = [ "video" "wheel" "networkmanager" "docker" ];
    hashedPassword = "$6$/RvS0Se.iCx$A0eA/8PzgMj.Ms9ohNamfu53c9S.zdG30hEmUHLjmWP0CaXTPVA6QxGIZ6fy.abkjSOTJMAq7fFL6LUBGs4BU0";
    shell = pkgs.zsh; #"/run/current-system/sw/bin/bash";
  };

  # For nix flakes
  nix.package = pkgs.nixFlakes;
  nix.extraOptions = "experimental-features = nix-command flakes";

  nix.allowedUsers =  [ "@wheel" "hhefesto" ];
  nix.trustedUsers = [ "root" "hhefesto" ];
}

This is the second third day I’ve tried to fix this without any luck. Any guidance is greatly appreciated.

Thank you very much for reading,
Daniel

edit: removed comments and trimmed down some of configuration.nix . I’m unsure of how much more I can trim it down: suggestions very welcomed.

I remember I had similar trouble with the switch to flakes as well, when I moved to flakes. It might not help a lot, or it might not even be relevant, but it was easier for me to make the flake setup a bit more hybrid with the non-flake one with this change instead of yours:

As for your issue, does the current nixos-rebuild you have, uses nixUnstable or nixStable? In general, if you have trouble with symlinks you can try to delete them or create them manually.

I would suggest to:

  • update the post by removing the commented out lines (will be easier for others to debug)
  • trim the flake.nix to the bare essentials - just a minimal working system - and go from there. If you don’t want to change your system’s configuration, build a VM with
nixos-rebuild build-vm --flake .#nixosConfigurations.Olimpo.config.system.build.toplevel

Hope this helps!

Thanks!

I’ve removed all comments, factored out configuration.nix and trimmed configuration.nix. I’m not sure how much more trimming I can do on that file.

I’m now booting from a usb and I’ve changed /etc/nixos/configuration.nix (which is different from /mnt/etc/nixos/configuration.nix) to:

{ config, pkgs, ... }:

{
  imports = [ <nixpkgs/nixos/modules/installer/cd-dvd/installation-cd-graphical-plasma5.nix> ];
  nix = {
    package = pkgs.nixFlakes;
    extraOptions = ''
      experimental-features = nix-command flakes
    '';
  };
  
}

After this I’ve done a successful # nixos-rebuild switch and then did:

# cd /mnt/etc/nixos/
# nix-shell -p nixFlakes
# nix build /mnt/etc/nixos#nixosConfigurations.olimpo.config.system.build.toplevel --experimental-features "flakes nix-command"
# nixos-install --root /mnt --system ./result

And I still get the same error:

...
copying path '/nix/store/yrs9wg63vz6a09s6m0q1idndddx3sk32-etc' to 'local'...
copying path '/nix/store/z1bvr5pd5d2dgxsng39q3qlrjhaxszn4-nixos-system-olimpo-21.05.20210228.0aeba64' to 'local'...
copying channel...
installing the boot loader...
removing group ‘vboxusers’
removing group ‘docker’
setting up /etc...
removing obsolete symlink ‘/etc/systemd/network/40-vboxnet0.link’...
Copied "/nix/store/4712fsjq670jsqjrpkkmpfzhi9g1k4nj-systemd-247.3/lib/systemd/boot/efi/systemd-bootx64.efi" to "/boot/EFI/systemd/systemd-bootx64.efi".
Copied "/nix/store/4712fsjq670jsqjrpkkmpfzhi9g1k4nj-systemd-247.3/lib/systemd/boot/efi/systemd-bootx64.efi" to "/boot/EFI/BOOT/BOOTX64.EFI".
Random seed file /boot/loader/random-seed successfully written (512 bytes).
Created EFI boot entry "Linux Boot Manager".
Traceback (most recent call last):
  File "/nix/store/6dmc2579q6dmmx0dkv4wc243q89n5mxq-systemd-boot-builder.py", line 256, in <module>
    main()
  File "/nix/store/6dmc2579q6dmmx0dkv4wc243q89n5mxq-systemd-boot-builder.py", line 223, in main
    remove_old_entries(gens)
  File "/nix/store/6dmc2579q6dmmx0dkv4wc243q89n5mxq-systemd-boot-builder.py", line 145, in remove_old_entries
    known_paths.append(copy_from_profile(*gen, "kernel", True))
  File "/nix/store/6dmc2579q6dmmx0dkv4wc243q89n5mxq-systemd-boot-builder.py", line 62, in copy_from_profile
    store_file_path = profile_path(profile, generation, name)
  File "/nix/store/6dmc2579q6dmmx0dkv4wc243q89n5mxq-systemd-boot-builder.py", line 59, in profile_path
    return os.readlink("%s/%s" % (system_dir(profile, generation), name))
FileNotFoundError: [Errno 2] No such file or directory: '/nix/var/nix/profiles/system-1-link/kernel'

Is this in Git so that we can see the whole picture in context? I’m not sure flakes is the issue, it seems like the derivation for your system toplevel is being built… but there’s a failure in activation because something the systemd-boot-builder is expecting is missing.

extra notes:

  • I’m not sure what happens if you leave off a stateVersion, but I wouldn’t recommend it.

Aha! This is probably the issue. Containers run under the “host” kernel, thus don’t have/need their own. But you’ve set this as a toplevel configuration and so the bootloader generator is going to try to generate boot entries for it.

(This is why I’m leary of people cutting down their configs when posting… flakes gives us the awesome power of being able to perfectly replicate what someone else is experiencing)

edit: But also, still go re-add the stateVersion too.

Separately, and assuming this is the culprit, can you share what example you found that in, so we can correct/make a note on it?

edit2: Here’s confirmation:

cole@slynux ~/etc-nixos-configuration main
❯ nix build .#nixosConfigurations.olimpo.config.system.build.toplevel

cole@slynux ~/etc-nixos-configuration main* 2m 50s
❯ ls ./result          
bin  specialisation  systemd   configuration-name  init                    nixos-version
etc  sw              activate  extra-dependencies  init-interface-version  system

no kernel ^

cole@slynux ~/etc-nixos-configuration main*
❯ nvim ./configuration.nix                                           

# remove isContainer

cole@slynux ~/etc-nixos-configuration main*
❯ nix build .#nixosConfigurations.olimpo.config.system.build.toplevel
warning: Git tree '/home/cole/etc-nixos-configuration' is dirty

cole@slynux ~/etc-nixos-configuration main* 24s
❯ ls ./result 
bin       kernel-modules  systemd                configuration-name  init-interface-version  kernel-params
etc       specialisation  activate               extra-dependencies  initrd                  nixos-version
firmware  sw              append-initrd-secrets  init                kernel                  system

now we have a kernel/initrd^

@colemickens Thank you! Finally my system is a flake!

Love your nixos-flake-example repo, thank you for that too.

I got boot.isContainer = true; from Eelco’s post. It’s probably correct for nixos-container but not for nixos-rebuild.

2 Likes