this post was submitted on 25 May 2024
10 points (100.0% liked)

Nix / NixOS

1736 readers
3 users here now

Main links

Videos

founded 1 year ago
MODERATORS
 

My solution:

let

  nixFilesInDirectory = directory:
    (
      map (file: "${directory}/${file}")
      (
        builtins.filter
          (
            nodeName:
              (builtins.isList (builtins.match ".+\.nix$" nodeName)) &&
              # checking that it is NOT a directory by seeing
              # if the node name forcefully used as a directory is an invalid path
              (!builtins.pathExists "${directory}/${nodeName}/.")
          )
          (builtins.attrNames (builtins.readDir directory))
      )
    );

  nixFilesInDirectories = directoryList:
    (
      builtins.concatMap
        (directory: nixFilesInDirectory directory)
        (directoryList)
    );
  # ...
in {
  imports = nixFilesInDirectories ([
      "${./programs}"
      "${./programs/terminal-niceties}"
  ]);
  # ...
}

snippet from the full source code: quazar-omega/home-manager-config (L5-L26)

credits:


I'm trying out Nix Home Manager and learning its features little by little.
I've been trying to split my app configurations into their own files now and saw that many do the following:

  1. Make a directory containing all the app specific configurations:
programs/
└── helix.nix
  1. Make a catch-all file default.nix that selectively imports the files inside:
programs/
├── default.nix
└── helix.nix

Content:

{
  imports = [
    ./helix.nix
  ];
}
  1. Import the directory (picking up the default.nix) within the home-manager configuration:
{
  # some stuff...
  imports = [
    ./programs
  ];
 # some other stuff...
}

I'd like to avoid having to write each and every file I'll create into the imports of default.nix, that kinda defeats the point of separating it if I'll have to specify everything anyway, so is there a way to do so? I haven't found different ways to do this in various Nix discussions.


Example I'm looking at: https://github.com/fufexan/dotfiles/blob/main/home/terminal/default.nix

My own repository: https://codeberg.org/quazar-omega/home-manager-config

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 5 points 5 months ago* (last edited 5 months ago) (1 children)

I do have a function that I cribbed from somewhere, unfortunately I didn't note down the source at the time so can't give them due credit here.

filterNixFiles = k: v: v == "regular" && lib.hasSuffix ".nix" k;

importNixFiles = path: (lib.lists.forEach (lib.mapAttrsToList (name: _: path + ("/" + name)) (lib.filterAttrs filterNixFiles (builtins.readDir path)))) import;

Then used as normal

modules = [ home-manager.nixosModules.home-manager vscode-server.nixosModule ] ++ (importNixFiles ./hosts/required) ++ (importNixFiles ./hosts/machines/${hostname})

Edit

Apologies for formatting on mobile

[–] [email protected] 2 points 5 months ago* (last edited 5 months ago)

Haha, I read this just after trying to figure it out on my own and it seems the solution is kind of similar to what I got, but this looks like it's probably more robust since it extracts it all into a function (though I have yet to understand the syntax 👀). So thanks, I'll be trying that as well!