Misterio77 / nix-starter-configs

Simple and documented config templates to help you get started with NixOS + home-manager + flakes. All the boilerplate you need!
Creative Commons Zero v1.0 Universal
2.24k stars 121 forks source link

What do I change if I want to have my configuration in my home directory instead of /etc? #66

Open ckp95 opened 5 months ago

ckp95 commented 5 months ago

I have not moved to flakes yet. Right now I keep my configuration.nix and hardware.nix under my home directory at ~/me/src/dotfiles. In my configuration.nix, I have set my nixPath as:

  nix.nixPath = [
    "nixos-config=/home/cameron/me/src/dotfiles/configuration.nix"
    "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
    "/nix/var/nix/profiles/per-user/root/channels"
  ];

so when I run sudo nixos-rebuild switch it uses the config there rather than looking in /etc/nixos. (I don't know what the channels-related ones are actually doing, I just kept them there from the original config)

I want to keep this directory layout in a flake-based config. I notice in the template, this section:

  # This will additionally add your inputs to the system's legacy channels
  # Making legacy nix commands consistent as well, awesome!
  nix.nixPath = ["/etc/nix/path"];
  environment.etc =
    lib.mapAttrs'
    (name: value: {
      name = "nix/path/${name}";
      value.source = value.flake;
    })
    config.nix.registry;

What is this /etc/nix/path doing? I don't currently have such a directory. Will it be created automatically? And do I need to add my dotfiles directory to this list?

Misterio77 commented 2 months ago

Hey @ckp95, apologies for the late response!

The NIX_PATH (here configured by nix.nixPath) is where nix will look when you have a angle bracket lookup.

For example, nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos will tell nix to use a channel named nixos owned by root when looking up <nixpkgs>. While /nix/var/nix/profiles/per-user/root/channels will tell nix to look for a channel with the same name for any other angle bracket it finds (e.g. <foo> will be looked up in /nix/var/nix/profiles/per-user/root/channels/foo).

When using flakes, you usually want to move away from channels, as they're effectively replaced with flake inputs.

The nix path angle bracket look up is still useful, though, as sometimes you want to evaluate non-flake nix files, which will still use the angle bracket syntax to lookup stuff (mostly nixpkgs).

In this template, we point NIX_PATH to /etc/nix/path, and create a /etc/nix/path/foobar for each flake input you have (e.g. /etc/nix/path/nixpkgs). Yep, this directory is created if it doesn't exist already.

This lets you use the exact inputs you have locked in your flake with the "legacy" angle brackets (global nix path) syntax.


The nixos-config nix path only matters to nixos-rebuild, when it's called without a --flake argument.

nixos-rebuild looks it up to find where your config is (falling back to /etc/nixos/configuration.nix).

When building your system with flakes, you will specify the path to your config with --flake, so the nixos-config nix path does not matter at all.

Misterio77 commented 2 months ago

In practice, the only angled bracket lookup that's actually useful outside of your config is <nixpkgs>, so we plan on simplifying this on the template to make things clearer.