Skip to content

Instantly share code, notes, and snippets.

@yunfachi
Last active December 10, 2023 17:10
Show Gist options
  • Save yunfachi/6c073cc6edc18f78cfc60bb9bb3f7143 to your computer and use it in GitHub Desktop.
Save yunfachi/6c073cc6edc18f78cfc60bb9bb3f7143 to your computer and use it in GitHub Desktop.
Nix / NixOS : Import all files from current directory
{...}: {
# imports by full path without copying to /nix/store
imports = builtins.map (n: toString ./. + "/${n}") (builtins.attrNames (builtins.removeAttrs (builtins.readDir ./.) [(builtins.unsafeGetAttrPos "_" {_ = null;}).file]));
# copies all files from the current directory to /nix/store and imports from /nix/store
# imports = builtins.map (n: "${./.}/${n}") (builtins.attrNames (builtins.removeAttrs (builtins.readDir ./.) [(builtins.unsafeGetAttrPos "_" {_ = null;}).file]));
}
@yunfachi
Copy link
Author

yunfachi commented Dec 10, 2023

a more advanced implementation of this thing
https://github.com/yunfachi/nixpkgs-yunfachi/blob/master/lib/umport.nix

{umport, ...}: {
  imports = umport {path = ./.;};
}
{umport, ...}: {
  imports = umport {
    path = ./.; # a required parameter that must be specified in the current file
    include = [../../bar.nix]; # default []
    exclude = [./foo.nix]; # defaults to [], but always excludes the current file
    copyToStore = true; # default false
  };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment