Umport: Automatic Import of Modules

Automatic import of all necessary modules with support for recursion and copying to /nix/store (optional), instead of default.nix with imports in each folder

ylib.umport {
  path ? null, # path (required parameter if paths is [])
  paths ? [], # multiple paths
  include ? [], # file paths that will be included anyway
  exclude ? [], # file paths that will be excluded anyway
  copyToStore ? false, # copy found files to /nix/store before importing them
  recursive ? false, # recursively import a path or paths
};

Usage

flake.nix

inputs.nypkgs.url = "github:yunfachi/nypkgs";
nixosConfigurations.<configuration> = lib.nixosSystem {
  # just a use case
  modules = 
    modules =
      [
        ./configuration.nix
      ]
      ++ ylib.umport {
        paths = [../programs ../services];
        recursive = true;
      };

  specialArgs = {
    ylib = nypkgs.lib.<system>;
    /* if you want to use ypkgs
    ypkgs = nypkgs.legacyPackages.<system>;
    ylib = ypkgs.lib; */
  };
}

default.nix

# just a use case
{ylib, ...}: {
  imports = ylib.umport {
    paths = [../programs ../services];
    recursive = true;
  };
}

Examples

Examples with execution result and tree
ylib.umport {
  paths = [./programs ./services];
  recursive = true;
};
#=> [ "/path/programs/cli/more/end.nix" "/path/programs/cli/default.nix" "/path/programs/cli/raboof.nix" "/path/programs/fuga.nix" "/path/programs/hoge.nix" "/path/services/bar.nix" "/path/services/eric.nix" "/path/services/foo.nix" ]
/path
β”œβ”€β”€ programs
β”‚   β”œβ”€β”€ cli
β”‚   β”‚   β”œβ”€β”€ default.nix
β”‚   β”‚   β”œβ”€β”€ more
β”‚   β”‚   β”‚   └── end.nix
β”‚   β”‚   └── raboof.nix
β”‚   β”œβ”€β”€ fuga.nix
β”‚   └── hoge.nix
└── services
    β”œβ”€β”€ bar.nix
    β”œβ”€β”€ eric.nix
    └── foo.nix



ylib.umport {
  path = ./programs;
  include = [./services/foo.nix ./services/bar.nix];
  exclude = [./programs/hoge.nix];
};
#=> [ "/path/programs/cli" "/path/programs/fuga.nix" "/path/services/foo.nix" "/path/services/bar.nix" ]
/path
β”œβ”€β”€ programs
β”‚   β”œβ”€β”€ cli
β”‚   β”‚   └── default.nix (when importing the directory, default.nix will be imported)
β”‚   └── fuga.nix
└── services
    β”œβ”€β”€ bar.nix
    └── foo.nix



ylib.umport {
  paths = [./programs ./services];
  include = [./default.nix];
  copyToStore = true;
  recursive = true;
};
#=> [ "/nix/store/kzx79d3j4hjcqk2yxvl153xmqh5qlvw4-more/end.nix" "/nix/store/pa64cc7vv8m1zvf6w4s9hvh4yz3hcx1f-default.nix" "/nix/store/py7029p6vcarj19mn8a8mbcqxjz4a318-cli/default.nix" "/nix/store/py7029p6vcarj19mn8a8mbcqxjz4a318-cli/raboof.nix" "/nix/store/y6ljmqzqrqka3y9mqkb0sp2r6d80dznv-programs/fuga.nix" "/nix/store/y6ljmqzqrqka3y9mqkb0sp2r6d80dznv-programs/hoge.nix" "/nix/store/l3sn7p8mlb95zdlv0mwnqdiyy6k1adhb-services/bar.nix" "/nix/store/l3sn7p8mlb95zdlv0mwnqdiyy6k1adhb-services/eric.nix" "/nix/store/l3sn7p8mlb95zdlv0mwnqdiyy6k1adhb-services/foo.nix" "/nix/store/pa64cc7vv8m1zvf6w4s9hvh4yz3hcx1f-default.nix" ]
/
└── nix
    └── store
        β”œβ”€β”€ kzx79d3j4hjcqk2yxvl153xmqh5qlvw4-more
        β”‚   └── end.nix
        β”œβ”€β”€ l3sn7p8mlb95zdlv0mwnqdiyy6k1adhb-services
        β”‚   β”œβ”€β”€ bar.nix
        β”‚   └── foo.nix
        β”œβ”€β”€ pa64cc7vv8m1zvf6w4s9hvh4yz3hcx1f-default.nix
        β”œβ”€β”€ py7029p6vcarj19mn8a8mbcqxjz4a318-cli
        β”‚   β”œβ”€β”€ default.nix
        β”‚   └── raboof.nix
        └── y6ljmqzqrqka3y9mqkb0sp2r6d80dznv-programs
            β”œβ”€β”€ fuga.nix
            └── hoge.nix

Why is this needed?

If you have a lot of modules or packages, then you can automatically call or import them using this, but only if you find it more convenient than regular imports

GitHub: GitHub - yunfachi/nypkgs: yunfachi's Nix Packages collection

9 Likes

This could be a great application of the file set library, which could potentially reduce this tool to ~10% of its size. Right now the file set library only provides toSource as a usable representation (which seems to correspond to the copyToStore behavior), but one could fairly simply add a transformation to a list of paths.

7 Likes

How easy would it be to create the transformation to a list of paths without accessing the library’s internal functions?

Maybe it should be included in the fileset library itself?

One of the most common problems I come across in nixos configuration repos is the burden of importing nix files recursively.
digga’s rakeLeaves and haumea are often used to solve this problem but now that lib.filesets exists they seem like (reinventing the wheel)/overkill.

3 Likes

Indeed there currently is no way to turn a fileset into a list of paths, but that’s only because I wasn’t necessary until now. I think it would be very useful to be able to do that now that I’m seeing this post :smiley:

Could you open a new issue in Nixpkgs to request this and ping me? You can also link it to File set library tracking issue and feature requests Β· Issue #266356 Β· NixOS/nixpkgs Β· GitHub

6 Likes