How do I create folders declaratively?

What I want is when I run nixos-rebuild for it to check if certain folders don’t exist and if they do create them. Specifically I want to do this to achieve two things:

  1. When I deploy a new system based on my config I want my home directory structured the way I like.
  2. I want a place to mount drives from which I can them sym link them into the home folder. The way I currently have my drives setup means that I cannot simply mount them into the home directory.

I have tried both pkgs.runCommand and with a shell script home manager’s home.file. The home manager solution did nothing and run command kept giving my a syntax error despite to the best of my knowledge exactly copying the syntax from the documentation.

I should note that I am completely new to nix and I am finding the documentation very confusing. Any explanation would be greatly appreciated.

The home manager solution I tried looked as follows:

home.file.git.enable = true;                                   #I am not sure if this is strictly necessary
home.file.git.recursive = true;                               #If I understand the documentation correct this turns the file into a folder
home.file.git.source = "/etc/nixos/empty-folder";  #This is the template of what to craete
home.file.git.target = "git";                                    #This is where to put that I have created

The runCommand solution I tried looks as follows:
pkgs.runCommand "create-folders" {} '' /etc/nixos/create-folders.sh '';

1 Like

Try with home.activation in home manager https://rycee.gitlab.io/home-manager/options.html#opt-home.activation

1 Like

Thank you very much.

For anyone else following in my footsteps the syntax is as follows:

home.activation.name = lib.hm.dag.entryAfter [ "writeBoundary" ] ''
    bash your-script.sh
  '';

“name” can be what ever you want it to be.
In addition you till need to add lib to the top of the nix file to it will look like { lib, config, pkgs, ... }:.

1 Like

The other way is to add entries to systemd tmpfiles; there are a few entry points as suits your need:

  • systemd.tmpfiles.rules, a list of lines in systemd tmpfiles.d format
  • systemd.tmpfiles.settings, a more nix-structured way to specify the same
  • systemd.user.tmpfiles.users.<name>.rules, a way to add such rules to a per-user unit
3 Likes