Importing multiple files from one directory, syntax

Good Afternoon,

I’m working on modularising my nixos configuration, but I’m running into some problems when performing my imports. I’d like to do something like this:

{config, pkgs, lib, ...}:
let 
  components = with .../components/general; [
    /base-config.nix
    /graphical.nix
    /networking.nix
    /sound.nix
    /bluetooth.nix
    /printing.nix
  ];
  programs = with .../programs; [
    /core.nix
    /cad.nix
    /devkit.nix
    /gaming.nix
  ];
  users = with .../users; [
    /$user.nix
  ];
in 

{
  imports = components ++ programs ++users;
}

All the file paths are correct, but the with statements are not working with file paths, so I assume I’m doing this the wrong way. Is there a correct way to do this other than typing out the full path for each file manually?

Thanks.

Add a default.nix to the folder an there import your modules.

configuration.nix

{
   imports = [ ./components ./programs ./users ];
}

./components/default.nix

{
  imports = [
    ./base-config.nix
    ./graphical.nix
    ./networking.nix
    ./sound.nix
    ./bluetooth.nix
    ./printing.nix
  ];
}

with keyword exposes an attr, ie:

let 
  programsPaths = { 
     core = ./programs/core.nix; 
     cad  = ./programs/cad.nix;
  };
  programs = with programsPaths; [ core cad ];
in
{
  imports = programs ++ compontens ++ users;
};

Would be easier just add paths without with

You could use let after equals and concat vars

let 
  components = let p = ../components/general; in [
    (p + "/base-config.nix")
    (p + "/graphical.nix")
  ];
in 
{
  imports = components ++ programs ++users;
}

I would prefer default.nix version :blush:

In your example has ... in nix parent dir is ../, current dir is ./.

There is also Haumea. to convert your dir structure in options. But to your case would be too complex.

1 Like