Help getting started with flakes

Greets Nixers,

I have successfully built NixOS with home-manager managing Alacritty. I’m taking baby steps and wanted to understand home-manager and how it can manage my config files. So far so good.

Now I am trying to move to flakes. I added a flake.nix file and copied over my configuration files to a directory I’ve named ‘flakes’. But I am running into two issues. 1) my git tree is dirty and I tried to git add ., but to no avail and 2) I’m getting an error with the way I have home-manager as a module configured (I think). Most likely user error. I tried searching, manuals, pills, etc., but have been unable to solve on my own. Your help is appreciated!

Here are my files for reference:

[leeuwarden@nixos:~/flake]$ sudo nixos-rebuild switch --flake .#
[sudo] password for leeuwarden:
warning: Git tree '/home/leeuwarden' is dirty
warning: creating lock file '/home/leeuwarden/flake/flake.lock'
warning: Git tree '/home/leeuwarden' is dirty
error: cannot look up '<home-manager/nixos>' in pure evaluation mode (use '--impure' to override)

       at /nix/store/ms1psi8r5axcn1h4kk136n109p5cn0fq-source/flake/configuration.nix:34:7:

           33|       ./kde.nix
           34|       <home-manager/nixos>
             |       ^
           35|
(use '--show-trace' to show detailed location information)

Git will consider any uncommitted changes “dirty”. That means, git add, and then git commit your changes. Then nix will stop complaining.

This is not an error, by the way, it’s just nix reminding you that you haven’t yet committed your changes, and that therefore your build cannot be reproduced from the repository. Nix will very happily build dirty trees, you can safely ignore that message until you’re done making changes.

Frankly, I’m not sure that warning is required, but I suppose it’s nice to avoid confusion with automated jobs.

Yep, pretty much. Flakes aren’t very well documented yet, nor is that bit of syntax.

What <home-manager/nixos> means is “nix, please look inside $NIX_PATH and find the path of home-manager, then the nixos file in it”. Since $NIX_PATH is an environment variable that can change every build, it’s impure and causes your build to be non-reproducible. Yes, this has always been a problem, flakes intend to fix this kind of wart.

This also means nix cannot cache the results of evaluating <home-manager/nixos>, which is one of the goals of flakes, so this is prohibited by default (though you can pass --impure like the error message suggests if you want to do this temporarily anyway).

So how are you supposed to get your dependencies into the evaluation? Well, you have to add a flake input for them, and pass it through all the way to where you use it.

The home-manager module has a nice explanation of how to do this here: Home Manager Manual

Shout if you still need a hand after reading through that.

1 Like

Thanks for the link @TLATER .

You are right. And both your comment and the nix message is appreciated. I’m just started learning git too, which doesn’t help.

I tried to change my flake.nix to mimic the Home Manager Manual module. It’s updated on my gitea and in my ~/flake folder on my system:

I did the following after, but still haven’t been able to rebuild:

[leeuwarden@nixos:~/flake]$ sudo git add .
[sudo] password for leeuwarden:

[leeuwarden@nixos:~/flake]$ sudo git commit -m "Updated flake.nix"
[main f57544c] Updated flake.nix
 2 files changed, 6 insertions(+), 20 deletions(-)

[leeuwarden@nixos:~/flake]$ sudo nixos-rebuild switch --flake .#
warning: Git tree '/home/leeuwarden' is dirty
error: flake 'git+file:///home/leeuwarden?dir=flake' does not provide attribute 'packages.x86_64-linux.nixosConfigurations."nixos".config.system.build.nixos-rebuild', 'legacyPackages.x86_64-linux.nixosConfigurations."nixos".config.system.build.nixos-rebuild' or 'nixosConfigurations."nixos".config.system.build.nixos-rebuild'
1 Like

This means, that nixos-rebuild searches for a configuration named nixos, as that is the current hosts hostname, though you only provide a config named hostname so you have to specify it manually like this:

$ sudo nixos-rebuild switch --flake .#hostname

Alternatively change the attribute name to match your hosts hostname, instead of hostname.

Thanks @Nobbz.

So I changed my hostname to nixos and ran the following, but I’m still doing something wrong and I am not seeing it. Reviewing Home Manager Manual looks like it should work to me, but I must be missing something.

After editing my flake.nix file:

[leeuwarden@nixos:~/flake]$ sudo git add .

[leeuwarden@nixos:~/flake]$ sudo git commit -m "Changed hostname to 'nixos'"
[main 78a3650] Changed hostname to 'nixos'
 1 file changed, 1 insertion(+), 1 deletion(-)

[leeuwarden@nixos:~/flake]$ sudo nixos-rebuild switch --flake .#
warning: Git tree '/home/leeuwarden' is dirty
error: flake 'git+file:///home/leeuwarden?dir=flake' does not provide attribute 'packages.x86_64-linux.nixosConfigurations."nixos".config.system.build.nixos-rebuild', 'legacyPackages.x86_64-linux.nixosConfigurations."nixos".config.system.build.nixos-rebuild' or 'nixosConfigurations."nixos".config.system.build.nixos-rebuild'

[leeuwarden@nixos:~/flake]$ sudo nixos-rebuild switch --flake .#nixos
warning: Git tree '/home/leeuwarden' is dirty
error: flake 'git+file:///home/leeuwarden?dir=flake' does not provide attribute 'packages.x86_64-linux.nixosConfigurations."nixos".config.system.build.nixos-rebuild', 'legacyPackages.x86_64-linux.nixosConfigurations."nixos".config.system.build.nixos-rebuild' or 'nixosConfigurations."nixos".config.system.build.nixos-rebuild'

I’m realizing this only now. In the last version you did not have the nesting under nixosConfigurations.

You need to nest system configuration below that.

1 Like

Thanks @NobbZ , I must have overwritten it in the last version. I added nixosConfigurations, now viewable in my git repo.

Then I tried the following:

[leeuwarden@nixos:~/flake]$ sudo git add .

[leeuwarden@nixos:~/flake]$ sudo git commit -m "Added nixosConfiguration"
[main 6d40ba3] Added nixosConfiguration
 1 file changed, 1 insertion(+), 1 deletion(-)

[leeuwarden@nixos:~/flake]$ sudo nixos-rebuild switch --flake .#nixos
warning: Git tree '/home/leeuwarden' is dirty
building the system configuration...
warning: Git tree '/home/leeuwarden' is dirty
error: getting status of '/nix/store/9lyjkyjqjm16hcap90409h1ics5bvsmj-source/flake/home.nix': No such file or directory
(use '--show-trace' to show detailed location information)

I added a home.nix configuration file to my repo too. But after trying:

[leeuwarden@nixos:~/flake]$ sudo git commit -m "Edited configs"
[main 3bed778] Edited configs
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 flake/home.nix

[leeuwarden@nixos:~/flake]$ sudo nixos-rebuild switch --flake .#nixos
warning: Git tree '/home/leeuwarden' is dirty
error: cannot look up '<home-manager/nixos>' in pure evaluation mode (use '--impure' to override)

       at /nix/store/mb3xkcssgnpwl19mbnw1b4lh561n4hh8-source/flake/configuration.nix:34:7:

           33|       ./kde.nix
           34|       <home-manager/nixos>
             |       ^
           35|
(use '--show-trace' to show detailed location information)

I tried commenting out the <home-manager/nixos> by itself as well as with related fields, sudo git add . and commits and rerunning sudo nixos-rebuild switch --flake .#nixos and keep getting similar warnings about home.nix not being a file or directory. I tried --show-trace to see if I could understand if anything else was causing this warning, but haven’t been able to figure it out yet. I will try to backtrack my steps using the Home-Manager Guide tomorrow to see what I have missed.

If you push that commit to a new branch and share that it’ll be easier to spot what’s going wrong :slight_smile:

For what it’s worth, the configuration in your repository right now looks almost correct to me. You should however remove this line: NixOS-Desktop_Flake_Testing/configuration.nix at master - NixOS-Desktop_Flake_Testing - Gitea: Git with a cup of tea

That’s already imported here, so doesn’t need to be imported again. Remember, using <> is forbidden when you use flakes: NixOS-Desktop_Flake_Testing/flake.nix at master - NixOS-Desktop_Flake_Testing - Gitea: Git with a cup of tea

Sorry @TLATER , I’m learning git as I go too. I can git init, add, push the initial repo, but am running into some issues that are on me after the initial upload. More for me to learn.

I did comment out <home-manager/nixos> and tried running nixos-rebuild switch --flake .#nixos yesterday and still received a warning. I will run it later and post the output.

@TLATER, I created a new repo, because my .git was not building in my /home/leeuwarden/flake folder. I’m not sure why it was building one directory up. It’s going to take me some time to figure out what I did incorrectly here as I was unable to push to the main branch, so I created a new repo in the meantime. Please bear with me as I am learning. I am going to review the correct way to add branches next to make it easier on those viewing my files (like you). :slight_smile:

After trying to rebuild, I am now getting a pure evaluation warning. I have read some other forums and webpages about overriding this, but before I do that, is there a way to remove the warning on my git tree being dirty and to fix this pure evaluation mode warning?

[leeuwarden@nixos:~/flake]$ sudo nixos-rebuild switch --flake .#nixos
warning: Git tree '/home/leeuwarden/flake' is dirty
building the system configuration...
warning: Git tree '/home/leeuwarden/flake' is dirty
error: cannot look up '<nixpkgs>' in pure evaluation mode (use '--impure' to override)

       at /nix/store/w3iz7x1gglk17xsz409yk1l7a4w90glj-source/default.nix:1:17:

            1| { pkgs ? import <nixpkgs> { }
             |                 ^
            2| , lib ? import <nixpkgs/lib>
(use '--show-trace' to show detailed location information)

Edit: Ignore all of what this said earlier, the error is coming from here: flake/configuration.nix at main - flake - Gitea: Git with a cup of tea

I will try to rebuild after removing the nixsoftware center. I don’t use it anyway and TBH, I think it has been defunct for some time on my system. Will post results later. Thanks.

That will be easier than explaining flake inputs in more detail for the moment :slight_smile:

There is one other issue I’m seeing - currently you import three different versions of home-manager in three places:

  1. flake/configuration.nix at main - flake - Gitea: Git with a cup of tea
  • This way is completely incompatible with pure evaluation; It uses the impure <path> syntax, therefore remove this line. For reference, this will import the version of home-manager you specified with nix-channel. Channels are bad because they are different on each computer.
  1. flake/home-manager.nix at main - flake - Gitea: Git with a cup of tea
  • This way could be made compatible with pure evaluation by specifying a checksum, but flakes provide a different mechanism for downloading other flakes. Since this isn’t going through that, it’s kind of defying the purpose of using flakes. In other words, remove this line, as well as lines 6-9 in that file. For reference, this will download and then import the version of home-manager you specify in that URL - whatever master.tar.gz happens to point to. It’s a bad idea to use this one way or another since that will change basically daily, so you can’t rely on it even with a checksum.
  1. flake/flake.nix at main - flake - Gitea: Git with a cup of tea
  • This is the correct way of importing home-manager with flakes. The variable specified there refers back to line 11, which in turn gets its input from line 7. So keep this line. For reference, this will use the version of home-manager defined in your flake.lock. Since that’s in your repository, it will stay the same regardless of where you evaluate your config. This is much better than the other two approaches - if you want to update, do so explicitly using nix flake update.

I don’t spot anything else that could cause issues, but I haven’t actually tried running your code, so I might miss something. If it doesn’t work for you by then I’ll happily give fixing it up a go, and share the resulting commit.

You can probably make nixsoftware center work by doing something similar to 3. I’ll happily explain that, and how this all works, once you’ve got the basic NixOS+home-manager set up working.

1 Like

Success! :grinning_face_with_smiling_eyes: Thank you @TLATER and @NobbZ for teaching me this valuable lesson on flakes. I hope to pass along what I have learned to anyone interested that I come across.

Here is the output for anyone that may be interested as well as for me to reference. sn is my alias for sudo nano in which I edited the config files per @TLATER .

[leeuwarden@nixos:~]$ cd flake

[leeuwarden@nixos:~/flake]$ ls
communication.nix  flake.nix   hardware-configuration.nix  internet.nix  media.nix   system.nix
configuration.nix  fonts.nix   home-manager.nix            kde.nix       office.nix
flake.lock         gaming.nix  home.nix                    laptop.nix    README.md

[leeuwarden@nixos:~/flake]$ sn configuration.nix
[sudo] password for leeuwarden:

[leeuwarden@nixos:~/flake]$ sn home-manager.nix

[leeuwarden@nixos:~/flake]$ sn home-manager.nix

[leeuwarden@nixos:~/flake]$ sudo git add .

[leeuwarden@nixos:~/flake]$ sudo git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   configuration.nix
	modified:   home-manager.nix


[leeuwarden@nixos:~/flake]$ sudo git commit -m "Modified config and h-m"
[main e278177] Modified config and h-m
 2 files changed, 15 insertions(+), 14 deletions(-)

[leeuwarden@nixos:~/flake]$ sudo git log
commit e278177022c6a24c34cbb0fe1fcd2a19f3d72fd4 (HEAD -> main)
Author: zonsopkomst <ewagner@disroot.org>
Date:   Wed Nov 16 22:18:15 2022 -0600

    Modified config and h-m

commit 856485795cbdf1197dc5ad9067814e30d7ffaf45 (origin/main)
Author: zonsopkomst <ewagner@disroot.org>
Date:   Tue Nov 15 06:45:19 2022 -0600

    first commit

[leeuwarden@nixos:~/flake]$ sudo git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

[leeuwarden@nixos:~/flake]$ sudo git push -u origin main
Username for 'https://git.disroot.org': zonsopkomst
Password for 'https://zonsopkomst@git.disroot.org':
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 12 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 493 bytes | 493.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
remote: . Processing 1 references
remote: Processed 1 references in total
To https://git.disroot.org/Zonsopkomst/flake.git
   8564857..e278177  main -> main
branch 'main' set up to track 'origin/main'.

[leeuwarden@nixos:~/flake]$ sudo nixos-rebuild switch --flake .#nixos
building the system configuration...
error: undefined variable 'nix-software-center'

       at /nix/store/7b980w5cyr5rpkgy6gzy7q9kf3svdkg9-source/configuration.nix:114:5:

          113|     packages = with pkgs; [
          114|     nix-software-center
             |     ^
          115|     ];
(use '--show-trace' to show detailed location information)

[leeuwarden@nixos:~/flake]$ sn configuration.nix

[leeuwarden@nixos:~/flake]$ sudo git add .

[leeuwarden@nixos:~/flake]$ sudo git commit -m "Commented out line 114 nix-software-center"
[main b1603b5] Commented out line 114 nix-software-center
 1 file changed, 1 insertion(+), 1 deletion(-)

[leeuwarden@nixos:~/flake]$ sudo git push -u origin main
Username for 'https://git.disroot.org': zonsopkomst
Password for 'https://zonsopkomst@git.disroot.org':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 313 bytes | 313.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: . Processing 1 references
remote: Processed 1 references in total
To https://git.disroot.org/Zonsopkomst/flake.git
   e278177..b1603b5  main -> main
branch 'main' set up to track 'origin/main'.

[leeuwarden@nixos:~/flake]$ sudo nixos-rebuild switch --flake .#nixos
building the system configuration...
stopping the following units: accounts-daemon.service, systemd-udevd-control.socket, systemd-udevd-kernel.socket, systemd-udevd.service
NOT restarting the following changed units: systemd-fsck@dev-disk-by\x2duuid-DE0A\x2d21E9.service
activating the configuration...
setting up /etc...
reloading user units for leeuwarden...
setting up tmpfiles
reloading the following units: dbus.service
restarting the following units: home-manager-leeuwarden.service, nix-daemon.service, polkit.service
starting the following units: accounts-daemon.service, systemd-udevd-control.socket, systemd-udevd-kernel.socket

[leeuwarden@nixos:~/flake]$ sudo nix flake update
warning: updating lock file '/home/leeuwarden/flake/flake.lock':
• Updated input 'home-manager':
    'github:nix-community/home-manager/d20e3d070c78271356a2d5d73c01f1de94586087' (2022-11-08)
  → 'github:nix-community/home-manager/bc90de24d898655542589237cc0a6ada7564cb6c' (2022-11-13)
• Updated input 'nixpkgs':
    'github:nixos/nixpkgs/667e5581d16745bcda791300ae7e2d73f49fff25' (2022-11-07)
  → 'github:nixos/nixpkgs/85d6b3990def7eef45f4502a82496de02a02b6e8' (2022-11-15)
warning: Git tree '/home/leeuwarden/flake' is dirty

[leeuwarden@nixos:~/flake]$ sudo git add .

[leeuwarden@nixos:~/flake]$ sudo git commit -m "First nix flake update"
[main a121f3b] First nix flake update
 1 file changed, 6 insertions(+), 6 deletions(-)

[leeuwarden@nixos:~/flake]$ sudo git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

[leeuwarden@nixos:~/flake]$ sudo git push -u origin main
Username for 'https://git.disroot.org': zonsopkomst
Password for 'https://zonsopkomst@git.disroot.org':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 515 bytes | 515.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: . Processing 1 references
remote: Processed 1 references in total
To https://git.disroot.org/Zonsopkomst/flake.git
   b1603b5..a121f3b  main -> main
branch 'main' set up to track 'origin/main'.

[leeuwarden@nixos:~/flake]$ sudo nixos-rebuild switch --flake .#nixos
building the system configuration...
updating systemd-boot from 251.5 to 251.7
Read $KERNEL_INSTALL_LAYOUT from /etc/machine-info. Please move it to the layout= setting of /etc/kernel/install.conf.
Copied "/nix/store/q11j05nqg829dkvgc8851mscbzi2iq8n-systemd-251.7/lib/systemd/boot/efi/systemd-bootx64.efi" to "/boot/efi/EFI/systemd/systemd-bootx64.efi".
Copied "/nix/store/q11j05nqg829dkvgc8851mscbzi2iq8n-systemd-251.7/lib/systemd/boot/efi/systemd-bootx64.efi" to "/boot/efi/EFI/BOOT/BOOTX64.EFI".
stopping the following units: ModemManager.service, NetworkManager-wait-online.service, NetworkManager.service, accounts-daemon.service, alsa-store.service, audit.service, cpufreq.service, cups.service, cups.socket, docker.service, docker.socket, kmod-static-nodes.service, logrotate-checkconf.service, mount-pstore.service, network-local-commands.service, network-setup.service, nscd.service, power-profiles-daemon.service, resolvconf.service, rtkit-daemon.service, syncthing.service, systemd-modules-load.service, systemd-oomd.service, systemd-oomd.socket, systemd-sysctl.service, systemd-timesyncd.service, systemd-udevd-control.socket, systemd-udevd-kernel.socket, systemd-udevd.service, udisks2.service, upower.service
NOT restarting the following changed units: display-manager.service, getty@tty1.service, systemd-fsck@dev-disk-by\x2duuid-DE0A\x2d21E9.service, systemd-journal-flush.service, systemd-logind.service, systemd-random-seed.service, systemd-remount-fs.service, systemd-update-utmp.service, systemd-user-sessions.service, user-runtime-dir@1000.service, user@1000.service
activating the configuration...
setting up /etc...
restarting systemd...
reloading user units for leeuwarden...
setting up tmpfiles
reloading the following units: dbus.service, firewall.service, reload-systemd-vconsole-setup.service
restarting the following units: home-manager-leeuwarden.service, nix-daemon.service, polkit.service, systemd-journald.service
starting the following units: ModemManager.service, NetworkManager-wait-online.service, NetworkManager.service, accounts-daemon.service, alsa-store.service, audit.service, cpufreq.service, cups.socket, docker.socket, kmod-static-nodes.service, logrotate-checkconf.service, mount-pstore.service, network-local-commands.service, network-setup.service, nscd.service, power-profiles-daemon.service, resolvconf.service, rtkit-daemon.service, syncthing.service, systemd-modules-load.service, systemd-oomd.socket, systemd-sysctl.service, systemd-timesyncd.service, systemd-udevd-control.socket, systemd-udevd-kernel.socket, udisks2.service, upower.service
the following new units were started: NetworkManager-dispatcher.service, systemd-hostnamed.service, systemd-vconsole-setup.service
1 Like

Also fyi, since you moved to flakes, you can move that directory to somewhere in $HOME and change the user to yours. You can then still build with

sudo nixos-rebuild switch --flake .#nixos

But it’ll allow you to use vscode or whatever instead of nano, and you won’t need all this sudoing.

My rule of thumb is that if I need sudo for anything except switch or reading files for debugging, it’s probably bad practice.

Sorry for the late reply. I’ve been away from most technology for the past week. I really appreciate your help getting me started with flakes.

Your comment is very interesting. I’ll have to look into vscode. Was thinking of getting away from nano and actually installed neovim, but the learning curve is steep. I’m open to transitioning or maybe even finding something better as I go in that direction.

I do have my flake and configs in a directory /home/leeuwarden/flake. I think my next steps are to figure out how to pull my repo to a new machine, copy the initial configuration files to the home/user/flake directory, and rebuild. I’ve made a ‘hosts’ directory in my flake directory. Then desktop, laptop, parents, server, etc. and tried to clean up my configuration files a bit. It works, but I am still rebuilding by changing the names of the users and hosts manually. If you have any information or could point me in the right direction, it would be much appreciated.

I’ll figure out how to create a new branch and merge it into git in the next few days hopefully and maybe create a new topic in the forum if I run into trouble.

Thanks again!

In that case, run sudo chown <user>:users . in your git repository’s root so that all your files are owned by you, and stop using sudo git! Needing sudo to modify anything in your home directory is generally a sign of something having gone catastrophically wrong :wink:

As a heavy emacs user, I appreciate the use of the old venerable editors, but if you’re used to graphical interfaces and still learning a lot, especially when you don’t have much time, the overhead of learning vim/emacs will make this endeavor take a long time. Meanwhile nano is frustratingly inflexible and doesn’t help you do anything with nix.

vscode may be a web browser, poorly adjusted for keyboard-based editing, and eat gigabytes of memory, but it is a useful tool for development. I’d suggest using it even just to get syntax highlighting, but if you use Nix IDE - Visual Studio Marketplace with nil it will point out a lot of silly mistakes for you.

Of course, no better time to learn vim/emacs than when you will use them a lot… But be warned that it might take you some dedicated effort.

Congratulations- I think you just set me back ten years in learning vim/emacs by telling me about vscode with nix ide and nil. :rofl: I look forward to using these tools. I will also change my file ownership on my git root. :v: