I cannot make sense of flakes

Hi, I recognize that this will seem identical to this post about exactly the same issue but I have stared at these files for over an hour and I don’t understand what I am doing with flakes or home manager.

I have been using NixOS fairly lightly for a few months, and had tried to understand flakes before and I couldn’t, so I left it alone. Unfortunately, the Zen browser is not available in any way on NixOS without using a flake, so I was attempting to make them make sense. I was trying to follow this guide from Vimjoyer on youtube to set the flake up, and his guide for home manager after that. Below is my flake and the home.nix file I have but when i run

sudo nixos-rebuild switch --flake ~/myflake

I get an error message saying error: flake ‘path:/home/anakin/myflake’ 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 want to make this work, and I have read and re-read the solved post above and I can’t understand what I don’t get. I apologize for asking about something again that should be simple.

Flake.nix

{
  1   description = "my NixOS flake";
  2 
  3   inputs = {
  4     nixpkgs.url = "nixpkgs/nixos-unstable";
  5     home-manager = {
  6       url = "github:nix-community/home-manager";
  7       inputs.nixpkgs.follows = "nixpkgs";
  8     };
  9   };
 10 
 11   outputs = {self, nixpkgs, home-manager, ...}@inputs:
 12   let
 13     system = "x86_64-linux";
 14     pkgs = import nixpkgs {
 15       inherit system;
 16       config = {
 17         allowUnfree = true;
 18       };
 19     };
 20   in
 21   {
 22   nixosConfigurations = {
 23     anakin = nixpkgs.lib.nixosSystem {
 24       system = "x86_64-linux";
 25       specialArgs = {inherit inputs system; };
 26       modules = [
 27         ./configuration.nix
 28         { home-manager.users.anakin = ./home.nix; }
 29         ];
 30       };
 31     };
 32     };
 33   }

home.nix

 { config, pkgs, inputs, ... }:
  1 {
  2   home.username = "anakin";
  3   home.homeDirectory = "/home/anakin";
  4   home.stateVersion = "23.11"; 
  5 
  6   programs.home-manager.enable = true;
  7 }

Flakes have outputs. Some outputs are nixosConfigurations. When you call the command without specifying the output, nixos-rebuild tries to switch your system to the nixosConfigurations.{currentSystemName} configuration(see manual). Your flake declares a system called anakin, so you need to run sudo nixos-rebuild switch --flake ~/myflake#anakin.

2 Likes

If you put your flake under /etc/nixos/flake.nix, you are able to run nixos-rebuild without any arguments, provided you expose an output with the hostname of you host, you already did so (if the hostname of your host is anakin)

It will also allow to run that command from any dir on your system.

I think I’ve made it work now, thank you! I also implemented the suggestion made here and moved the flake.nix, flake.lock, and home.nix files into the /etc/nixos directory.

1 Like

Is there a reason it seemed like most guides were creating a new directory for their flakes in their user’s home directory instead of doing things this way? I had only made the ~/myflake directory because I had seen other users doing something similar. Is there a reason NOT to but my flake files in the /etc/nixos directory or does it mostly not matter?

I moved them on your recommendation and I think it works, at least until I break something new. Thank you for the help!

most guides were creating a new directory for their flakes in their user’s home directory

a flake can be located anywhere as long as you cd in to that directory and do nixos-rebuild switch --flake .#hostname

so like any other project, people are saving it in the home directory.

without flakes I think you can still customise your /etc/nixos/configuration.nix path but usually, no one bothers doing it.

Is there a reason NOT to but my flake files in the /etc/nixos directory or does it mostly not matter

it’s the default location nixos-rebuild will look for, its upto you if you want to use the default.

You can also symlink /etc/nixos/flake.nix (and only that file) to wherever it’s actually located.

I was attempting to follow the commands laid out in this link to symlink my /etc/nixos/ directory and that didn’t make much more sense. Sorry for the late reply

ln -s /path/to/your/config/flake.nix /etc/nixos/flake.nix

no need to symlink the dir.

as in the only link i need is for the flkae.nix file? No need to link the flake.lock or anything else?

That’s correct, nixos-rebuild will follow that symlink before reading any other files, and will put the whole directory where target is located into nix store before building anything. Long story short - one symlink works for whatever complex structure you have (well, possibly except import ..)

I think I have properly linked the flake and unlinked the rest of the directory otherwise. Now running sudo nixos-rebuild switch --flake .#anakin fails with this error

error: access to absolute path '/home' is forbidden in pure evaluation mode (use '--impure' to override)

edit: I was trying to enable multi-monitor support today and not being able to rebuild my system has basically hard-stopped all of the things I was attempting to fix or enable this week

The error seems self-explanatory. You have to keep your config together, you can’t use absolute paths to read config files.

I would look for anywhere you’ve used /home (copy everything in /home to the Nix store and replace this with the Nix store path) where you meant "/home" (a string containing “/home”), likely somewhere in home-manager.

1 Like

I wish it was self explanatory but I already feel so stupid. Whole config together? My whole config is in /etc/nixosnixos-config-files other than the flake, which is linked to this directory. The initial flake.nix flie is in ~/Nixos/flake.nix and is linked to /etc/nixos/flake.nix where my config lives.


If the flake can’t be there, then what was the point in mentioning it can be linked from anywhere to /etc/nixos/flake.nix. I don’t understand whats wrong because the only places /home anywhere are

  • In the path of the link flake.nix -> /home/anakin/Nixos/flake.nix
  • In home.nix, which looks like this:
{ config, pkgs, inputs, ... }:
{
  home.username = "anakin";
  home.homeDirectory = "/home/anakin";
  home.stateVersion = "23.11"; 
  
  programs.home-manager.enable = true;
}

My home.nix file looks like this, but I don’t understand what you mean about the Nix store/ Nix store path:

{ config, pkgs, inputs, ... }:
{
  home.username = "anakin";
  home.homeDirectory = "/home/anakin";
  home.stateVersion = "23.11"; 
  
  programs.home-manager.enable = true;
}

You have to move your entire config to /home/anakin/Nixos if you want to symlink it that way. That’s the reason for the error.

EDIT: corrected

I recommend to just leave it somewhere in your home directory, or subdirectory. This way you avoid the extra hassle of having to symlink paths and deal with directories that are sudo priviledged. Your can search for the last time rebuild command in your shell history when you want to rebuild it and you won’t have to worry to type it again, though it’s important you pay atention to which host you are switching to (--flake path#host).

That said, your flake it’s just another project that happens to be able to change your OS when ran properly, no one keeps personal projects under /etc/ (I think, lol). So keep it as a personal project, use it in a git repo and understand nixos-rebuild sub-commands.

But this is all very opinionated, do it as you like.

I have a full copy of my /etc/nixos/ directory in ~/Nixos/. I can cd into this directory and run sudo nixos-rebuild switch --flake .#anakin but that errors the same. I moved /etc/nixos/ to /etc/nixos.bk/ so that I don’t have anything in /etc/nixos/ at all but I have the same issue. Is there a path in the configuration that needs to be update to the new path that I am missing?

I apologize for the stupid questions

I have the flake, as well as moved my /etc/nixos/ directory to ~/Nixos/ I think I removed the link for flake.nix properly, but I am still erroring. After all this, I just want to get a setuip that works, and I’ll learn to use the system around where it needs to sit.