Help moving system configuration + home manager to a flake

Hey! :wave:

I’ve got some questions on moving a NixOS system configuration to a flake. I’m currently not able to make it work and I have some question about it in general.

Edit: I may have been able to solve the first issue by using a higher home.stateVersion (don’t think I had one before(?) and used the system.stateVersion). This seems to get past the first issue, but I’m now stuck at using overlays again :see_no_evil:

Having seen this video on moving your NixOS system configuration to a flake (which made it look real easy), I thought I’d give it a go myself. But in doing so, I encountered a few issues. I’ve managed to solve (I think?) some of them, but am now stuck. I was hoping I could get some help.

For reference, here’s the current state of the flake I’m using (tracking my efforts, so it may change).

For reference, here's the current state of the flake I'm using.
{
  description = "My Nixos system config";

  inputs =
    # let
    #   systemVersion = "22.05";
    #   # if system version is "unstable" use "master"; otherwise use "release-${systemVersion}"
    #   # homeManagerVersion = if systemVersion == "unstable" then "master" else "release-${systemVersion}";
    # in
    {
      # nixpkgs.url = "nixpkgs/nixos-${systemVersion}";
      nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
      home-manager.url = "github:nix-community/home-manager/master";
      home-manager.inputs.nixpkgs.follows = "nixpkgs";
      musnix.url = "github:musnix/musnix";
      emacsOverlay.url = "github:nix-community/emacs-overlay";
    };

  outputs = { nixpkgs, home-manager, musnix, emacsOverlay, ... }@inputs:
    let
      system = "x86_64-linux";

      pkgs = import nixpkgs {
        inherit system;
        config = {
          allowUnfree = true;
        };
      };

      lib = nixpkgs.lib;

    in
    {
      homeManagerConfigs = {
        phaaze = home-manager.lib.homeManagerConfiguration {
          pkgs = nixpkgs.legacyPackages.${system};
          # pkgs = import nixpkgs {
          #   overlays = [emacsOverlay.overlay];
          #   inherit system;
          # };

          modules = [
            ({
              nixpkgs.overlays = [ emacsOverlay.overlay ];
            })
            # ({ pkgs, ... }: {
            #   nixpkgs.overlays = [ emacsOverlay.overlay ];
            # })
            ./phaaze/home.nix
            {
              home = {
                username = "thomas";
                homeDirectory = "/home/thomas";
                stateVersion = "18.09";
              };
            }
          ];
        };
      };

      nixosConfigurations = {
        phaaze = lib.nixosSystem {
          inherit system;

          modules = [
            musnix.nixosModules.musnix
            ./phaaze/configuration.nix
          ];
        };
      };
    };
}

The current issue is that when I try to build the home manager package (using nix build .#homeManagerConfigs.phaaze.activationPackage), I get this message:

error: cannot look up '<nixpkgs>' in pure evaluation mode (use '--impure' to override)

       at /nix/store/bdk5advb7w9kfnv25z9j6xmmmqfy9sga-source/modules/modules.nix:301:11:

          300|         else
          301|           <nixpkgs>);
             |           ^
          302|       _module.args.pkgs = lib.mkDefault pkgs;
(use '--show-trace' to show detailed location information)

For most of the error messages I have found, I have been able to go and change the source file to adapt to the error message, but this doesn’t seem to be (directly) in my files at all. Following the file it points to, I do indeed find the line in question, but it looks auto-generated to me, and I don’t know how or if I can change it:

`modules.nix`, the file that errors
{ pkgs

# Note, this should be "the standard library" + HM extensions.
, lib

# Whether to enable module type checking.
, check ? true

  # If disabled, the pkgs attribute passed to this function is used instead.
, useNixpkgsModule ? true }:

with lib;

let

  modules = [
    ./accounts/email.nix
    ./config/home-cursor.nix
    ./config/i18n.nix
    ./files.nix
    ./home-environment.nix
    ./i18n/input-method/default.nix
    ./launchd/default.nix
    ./manual.nix
    ./misc/dconf.nix
    ./misc/debug.nix
    ./misc/fontconfig.nix
    ./misc/gtk.nix
    ./misc/lib.nix
    ./misc/news.nix
    ./misc/numlock.nix
    ./misc/pam.nix
    ./misc/qt.nix
    ./misc/specialization.nix
    ./misc/submodule-support.nix
    ./misc/tmpfiles.nix
    ./misc/version.nix
    ./misc/vte.nix
    ./misc/xdg-desktop-entries.nix
    ./misc/xdg-mime-apps.nix
    ./misc/xdg-mime.nix
    ./misc/xdg-system-dirs.nix
    ./misc/xdg-user-dirs.nix
    ./misc/xdg.nix
    ./programs/abook.nix
    ./programs/afew.nix
    ./programs/alacritty.nix
    ./programs/alot.nix
    ./programs/aria2.nix
    ./programs/astroid.nix
    ./programs/atuin.nix
    ./programs/autojump.nix
    ./programs/autorandr.nix
    ./programs/bash.nix
    ./programs/bat.nix
    ./programs/beets.nix
    ./programs/bottom.nix
    ./programs/broot.nix
    ./programs/browserpass.nix
    ./programs/chromium.nix
    ./programs/command-not-found/command-not-found.nix
    ./programs/dircolors.nix
    ./programs/direnv.nix
    ./programs/eclipse.nix
    ./programs/emacs.nix
    ./programs/eww.nix
    ./programs/exa.nix
    ./programs/feh.nix
    ./programs/firefox.nix
    ./programs/fish.nix
    ./programs/foot.nix
    ./programs/fzf.nix
    ./programs/getmail.nix
    ./programs/gh.nix
    ./programs/git.nix
    ./programs/gitui.nix
    ./programs/gnome-terminal.nix
    ./programs/go.nix
    ./programs/gpg.nix
    ./programs/helix.nix
    ./programs/hexchat.nix
    ./programs/himalaya.nix
    ./programs/home-manager.nix
    ./programs/htop.nix
    ./programs/hyfetch.nix
    ./programs/i3status-rust.nix
    ./programs/i3status.nix
    ./programs/info.nix
    ./programs/ion.nix
    ./programs/irssi.nix
    ./programs/java.nix
    ./programs/jq.nix
    ./programs/just.nix
    ./programs/kakoune.nix
    ./programs/keychain.nix
    ./programs/kitty.nix
    ./programs/kodi.nix
    ./programs/lazygit.nix
    ./programs/less.nix
    ./programs/lesspipe.nix
    ./programs/lf.nix
    ./programs/librewolf.nix
    ./programs/lieer.nix
    ./programs/lsd.nix
    ./programs/man.nix
    ./programs/mangohud.nix
    ./programs/matplotlib.nix
    ./programs/mbsync.nix
    ./programs/mcfly.nix
    ./programs/mercurial.nix
    ./programs/micro.nix
    ./programs/mpv.nix
    ./programs/msmtp.nix
    ./programs/mu.nix
    ./programs/mujmap.nix
    ./programs/navi.nix
    ./programs/ncmpcpp.nix
    ./programs/ncspot.nix
    ./programs/ne.nix
    ./programs/neomutt.nix
    ./programs/neovim.nix
    ./programs/newsboat.nix
    ./programs/nix-index.nix
    ./programs/nnn.nix
    ./programs/noti.nix
    ./programs/notmuch.nix
    ./programs/nushell.nix
    ./programs/obs-studio.nix
    ./programs/octant.nix
    ./programs/offlineimap.nix
    ./programs/opam.nix
    ./programs/pandoc.nix
    ./programs/password-store.nix
    ./programs/pazi.nix
    ./programs/pet.nix
    ./programs/pidgin.nix
    ./programs/pistol.nix
    ./programs/piston-cli.nix
    ./programs/powerline-go.nix
    ./programs/pubs.nix
    ./programs/pylint.nix
    ./programs/qutebrowser.nix
    ./programs/rbw.nix
    ./programs/readline.nix
    ./programs/rofi-pass.nix
    ./programs/rofi.nix
    ./programs/rtorrent.nix
    ./programs/sagemath.nix
    ./programs/sbt.nix
    ./programs/scmpuff.nix
    ./programs/senpai.nix
    ./programs/sioyek.nix
    ./programs/skim.nix
    ./programs/sm64ex.nix
    ./programs/sqls.nix
    ./programs/ssh.nix
    ./programs/starship.nix
    ./programs/swaylock.nix
    ./programs/taskwarrior.nix
    ./programs/tealdeer.nix
    ./programs/terminator.nix
    ./programs/termite.nix
    ./programs/texlive.nix
    ./programs/timidity.nix
    ./programs/tint2.nix
    ./programs/tiny.nix
    ./programs/tmux.nix
    ./programs/topgrade.nix
    ./programs/urxvt.nix
    ./programs/vim.nix
    ./programs/vscode.nix
    ./programs/vscode/haskell.nix
    ./programs/pywal.nix
    ./programs/watson.nix
    ./programs/waybar.nix
    ./programs/xmobar.nix
    ./programs/z-lua.nix
    ./programs/zathura.nix
    ./programs/zellij.nix
    ./programs/zoxide.nix
    ./programs/zplug.nix
    ./programs/zsh.nix
    ./programs/zsh/prezto.nix
    ./services/barrier.nix
    ./services/betterlockscreen.nix
    ./services/blueman-applet.nix
    ./services/caffeine.nix
    ./services/cbatticon.nix
    ./services/clipmenu.nix
    ./services/devilspie2.nix
    ./services/dropbox.nix
    ./services/dunst.nix
    ./services/dwm-status.nix
    ./services/easyeffects.nix
    ./services/emacs.nix
    ./services/etesync-dav.nix
    ./services/espanso.nix
    ./services/flameshot.nix
    ./services/fluidsynth.nix
    ./services/fnott.nix
    ./services/fusuma.nix
    ./services/getmail.nix
    ./services/git-sync.nix
    ./services/gnome-keyring.nix
    ./services/gpg-agent.nix
    ./services/grobi.nix
    ./services/gromit-mpx.nix
    ./services/home-manager-auto-upgrade.nix
    ./services/hound.nix
    ./services/imapnotify.nix
    ./services/kanshi.nix
    ./services/kbfs.nix
    ./services/kdeconnect.nix
    ./services/keepassx.nix
    ./services/keybase.nix
    ./services/keynav.nix
    ./services/lieer.nix
    ./services/lorri.nix
    ./services/mako.nix
    ./services/mbsync.nix
    ./services/mopidy.nix
    ./services/mpd.nix
    ./services/mpdris2.nix
    ./services/mpd-discord-rpc.nix
    ./services/mpris-proxy.nix
    ./services/muchsync.nix
    ./services/network-manager-applet.nix
    ./services/nextcloud-client.nix
    ./services/notify-osd.nix
    ./services/opensnitch-ui.nix
    ./services/owncloud-client.nix
    ./services/pantalaimon.nix
    ./services/parcellite.nix
    ./services/pass-secret-service.nix
    ./services/password-store-sync.nix
    ./services/pasystray.nix
    ./services/pbgopy.nix
    ./services/picom.nix
    ./services/plan9port.nix
    ./services/playerctld.nix
    ./services/plex-mpv-shim.nix
    ./services/polybar.nix
    ./services/poweralertd.nix
    ./services/pulseeffects.nix
    ./services/random-background.nix
    ./services/recoll.nix
    ./services/redshift-gammastep/gammastep.nix
    ./services/redshift-gammastep/redshift.nix
    ./services/rsibreak.nix
    ./services/screen-locker.nix
    ./services/sctd.nix
    ./services/spotifyd.nix
    ./services/stalonetray.nix
    ./services/status-notifier-watcher.nix
    ./services/swayidle.nix
    ./services/sxhkd.nix
    ./services/syncthing.nix
    ./services/systembus-notify.nix
    ./services/taffybar.nix
    ./services/tahoe-lafs.nix
    ./services/taskwarrior-sync.nix
    ./services/trayer.nix
    ./services/twmn.nix
    ./services/udiskie.nix
    ./services/unclutter.nix
    ./services/unison.nix
    ./services/volnoti.nix
    ./services/window-managers/awesome.nix
    ./services/window-managers/bspwm/default.nix
    ./services/window-managers/herbstluftwm.nix
    ./services/window-managers/i3-sway/i3.nix
    ./services/window-managers/i3-sway/sway.nix
    ./services/window-managers/i3-sway/swaynag.nix
    ./services/window-managers/spectrwm.nix
    ./services/window-managers/xmonad.nix
    ./services/wlsunset.nix
    ./services/xcape.nix
    ./services/xembed-sni-proxy.nix
    ./services/xidlehook.nix
    ./services/xscreensaver.nix
    ./services/xsettingsd.nix
    ./services/xsuspender.nix
    ./systemd.nix
    ./targets/darwin
    ./targets/generic-linux.nix
    ./xresources.nix
    ./xsession.nix
    ./misc/nix.nix
    (pkgs.path + "/nixos/modules/misc/assertions.nix")
    (pkgs.path + "/nixos/modules/misc/meta.nix")
  ] ++ optional useNixpkgsModule ./misc/nixpkgs.nix
    ++ optional (!useNixpkgsModule) ./misc/nixpkgs-disabled.nix;

  pkgsModule = { config, ... }: {
    config = {
      _module.args.baseModules = modules;
      _module.args.pkgsPath = lib.mkDefault
        (if versionAtLeast config.home.stateVersion "20.09" then
          pkgs.path
        else
          <nixpkgs>);
      _module.args.pkgs = lib.mkDefault pkgs;
      _module.check = check;
      lib = lib.hm;
    } // optionalAttrs useNixpkgsModule {
      nixpkgs.system = mkDefault pkgs.stdenv.hostPlatform.system;
    };
  };

in modules ++ [ pkgsModule ]

Does anyone know what this is about or what might be going on?

Extra questions

I have a few more questions relating to using flakes for system configuration. Some of them may or may not relate to the issue: I genuinely don’t know.

Nix expressions outside of outputs

From my understanding, you can’t do any sort of evaluation outside of the outputs attribute, is that correct? I wanted to put my system version into a variable and use that to update all paths in the inputs attribute, but using a let block, I was told that it got a thunk but expected a set. I think I found a github thread about this when searching, but it was definitely surprising. Is there any way about this? I’d like to set system version once and then populate the nixos and home-manager channels with the correct url based on that.

How do you use overlays?

(This may relate to the issue I’m having, but I don’t know.)

Up until now, I’ve used the Nix community emacs overlay to get my emacs version. I’ve imported it as an overlay with fetchTarball in my home-manager config. However, I thought I’d try and add the overlay to the flake instead (seems like the right thing to do), but I can’t find any good documentation on how to add overlays in flakes. I did manage to find this response in a thread here on discourse which gives a brief explanation on how to set it up. That seems to work, but it doesn’t say anything about how you reference the overlay in any of the other modules.

What I ended up doing was this:

        phaaze = home-manager.lib.homeManagerConfiguration {
          pkgs = nixpkgs.legacyPackages.${system};

          modules = [
            ({
              nixpkgs.overlays = [ emacsOverlay.overlay ];
            })
            ./phaaze/home.nix
            {
              home = {
                username = "thomas";
                homeDirectory = "/home/thomas";
                stateVersion = "18.09";
              };
            }
          ];
        };

and then referencing the version of emacs in my home manager module params:

{ pkgs, config, emacsNativeComp, ... }: # adding emacsNativeComp to the top of the file

let 
  # ... rest of the config

Is that how it’s supposed to be done? Or is there another way?

Update: The overlay method does not work

After updating the home.stateVersion I used, I got past the initial hurdle mentioned above. It now gets to the point where it seems to try and evaluate my home config, but it can’t find emacsNativeComp. So the overlay question is now the main one: how do you use them and reference them in pre-existing modules?


Thanks a lot for making it all the way down here! Any input (on any of the questions) would be very much appreciated :pray: If you need additional info, I would of course be happy to provide that too :grinning_face_with_smiling_eyes:

1 Like

This needs to be at least 20.09, everything older than that can not be used for HM flake configuration.

Thanks! :grinning_face_with_smiling_eyes: Yeah, I found that out (edited my post after). Don’t think I’ve ever set a version for home manager before, so I thought it should be the same as the system state version. Setting it to 22.11 got me past that hurdle, but also revealed that my overlay didn’t work after all. Would you know how to use overlays with flakes and home manager?

You add them to nixpkgs.overlays, this works for me just fine when building Emacs.

Cool. How do you do that? As mentioned in the post, I tried the following:

        phaaze = home-manager.lib.homeManagerConfiguration {
          pkgs = nixpkgs.legacyPackages.${system};

          modules = [
            ({
              nixpkgs.overlays = [ emacsOverlay.overlay ];
            })
            ./phaaze/home.nix
            {
              home = {
                username = "thomas";
                homeDirectory = "/home/thomas";
                stateVersion = "22.11";
              };
            }
          ];
        };

but I don’t know how to reference the overlay in the home manager module.

To be clear, I’m still using more or less the same home-manager config file as I was before flakes. Previously I’d created an unstable channel by fetching a tarball and adding an overlay. That way I could reference the emacs version as unstable.emacsNativeComp. Now, however, I don’t know how to reference the package in the home manager config file. Could you shed some light on that?

Hmm, oh, right. It just becomes part of the pkgs argument in that case. Yeah, that does seem to work, actually :partying_face:

How many times do I need to set allowUnfree? Can I set it once and have it apply for everything?

Once per import.

nixpkgs.config.allowUnfree should be applied to the passed in pkgs, though there is a known bug in HM/nixpkgs that makes it not work, as a workaround you can use nixpkgs.config.allowUnfreePredicate = _: true.

If you import another instance, you need to apply the configuration again.

Thanks! Yeah, I found mention of that workaround and that does seem to work.

Once per import.

I’m sorry, but I’m not sure I understand what that means. What is an import and how do you set it? I set this in a let block leading into outputs, but I don’t think I ever use the variable anywhere:

      pkgs = import nixpkgs {
        inherit system;
        config = {
          allowUnfree = true;
        };
      };

Does it get used for anything at all, then? And do you know what the

          pkgs = nixpkgs.legacyPackages.${system};

part of the home manager config is? Could I use that somehow?

Hosted by Flying Circus.