Referencing files relative to the root flake.nix

Heyo peeps,

I started creating a motherflake for my configuration, using home-manager to handle my packages and dotfiles:

flake.nix:

  description = "The master flake for Loutre Telecom";

  inputs = {
    nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable;
    home-manager.url = github:nix-community/home-manager;
    home-manager.inputs.nixpkgs.follows = "nixpkgs";

  };

  outputs = inputs@{ nixpkgs, home-manager, ...  }: {
    nixosConfigurations.axolotl = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
        ./nixos_configurations/common/workstation.nix
        ./nixos_configurations/hosts/axolotl.nix
        home-manager.nixosModules.home-manager
        {
          home-manager = {
            useGlobalPkgs = true;
            useUserPackages = true;
            users.chewie = import ./users/chewie.nix;
          };
        }
       ];
    };
  };
}

users/chewie.nix (the home.nix):

{ config, pkgs, ... }:
{
  home.username = "chewie";
  home.homeDirectory = "/home/chewie";
  home.stateVersion = "22.05";

  programs.home-manager.enable = true;

  home.packages = with pkgs; [
    vim
    htop
    wget
    rxvt-unicode
  ];

  # This line fails
  home.file.".vimrc".source = ./dotfiles/vim/.vimrc;

  xsession.enable = true;
  xsession.windowManager.awesome.enable = true;
}

However, when running sudo nixos-rebuild switch --flake '.#axolotl', I get the following error:

error: getting status of '/nix/store/37dbp92g4089bcs7chsp5x6qkhcc9mq0-source/users/dotfiles': No such file or directory

It seems that, since chewie.nix exists in the users directory, it expects my dotfiles directory to exist there instead of the root of my repo, which is hell on my OCD brain.

Any idea on how to make that file reference relative to the root of the flake?

Cheers,

I see 2 options (maybe there are other alternatives):

  • Use a let binding in your home.nix:
{ config, pkgs, ... }:
let rootPath = ../.; in 
{
home.file.".vimrc".source = rootPath + /dotfiles/vim/.vimrc;
}
  • Use the extraSpecialArgs to pass the rootPath:

In flake.nix:

home-manager.nixosModules.home-manager
        {
          home-manager = {
             extraSpecialArgs = {rootPath = ./.;};
          };

And in home.nix:

{ config, pkgs, rootPath, ... }:
...

You can also get the root path from the self input, using self.outPath or just by coercing it to a string with antiquotes. Since that gives a string with context, rather than a path type, in some cases you’ll need to trigger copying to the nix store explicitly with builtins.path, though.

1 Like

Thanks for your replies!
In the end, I sidestepped the issue by keeping my dotfiles in a separate git repo that is imported as an input, but researching this made me learn a lot!

1 Like