feat(lib): add helper lib.importPackages

This makes the repo initialization simpler (see examples)
This commit is contained in:
DavHau 2023-09-06 00:42:04 +02:00
parent 79fc0de519
commit 556f2e7114
3 changed files with 51 additions and 25 deletions

View File

@ -13,18 +13,14 @@
...
}: let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
lib = nixpkgs.lib;
packageModuleNames = builtins.attrNames (builtins.readDir ./packages);
in {
# all packages defined inside ./packages/
packages.${system} =
lib.genAttrs packageModuleNames
(moduleName:
dream2nix.lib.evalModules {
modules = ["${./packages}/${moduleName}" ./settings.nix];
packageSets.nixpkgs = pkgs;
specialArgs.self = self;
});
packages.${system} = dream2nix.lib.importPackages {
projectRoot = ./.;
# can be changed to ".git" or "flake.nix" to get rid of .project-root
projectRootFile = ".project-root";
packagesDir = "/packages";
packageSets.nixpkgs = nixpkgs.legacyPackages.${system};
};
};
}

View File

@ -7,17 +7,14 @@
}: let
dream2nix = import dream2nixSource;
nixpkgs = import dream2nix.inputs.nixpkgs {};
lib = nixpkgs.lib;
packageModuleNames = builtins.attrNames (builtins.readDir ./packages);
packages =
lib.genAttrs packageModuleNames
(moduleName:
dream2nix.lib.evalModules {
modules = ["${./packages}/${moduleName}" ./settings.nix];
packageSets.nixpkgs = nixpkgs;
});
# all packages defined inside ./packages/
packages = dream2nix.lib.importPackages {
projectRoot = ./.;
# can be changed to ".git" to get rid of .project-root
projectRootFile = ".project-root";
packagesDir = "/packages";
packageSets.nixpkgs = nixpkgs;
};
in
# all packages defined inside ./packages/
packages

View File

@ -7,16 +7,49 @@
flake.options.lib = lib.mkOption {
type = lib.types.lazyAttrsOf lib.types.raw;
};
flake.config.lib.importPackages = args @ {
projectRoot,
projectRootFile,
packagesDir,
...
}: let
packagesDirPath =
if ! builtins.isString packagesDir
then throw "packagesDir must be a string"
else projectRoot + "/${packagesDir}";
forwardedArgs = builtins.removeAttrs args [
"projectRoot"
"projectRootFile"
"packagesDir"
];
in
lib.mapAttrs
(
module: type:
self.lib.evalModules (forwardedArgs
// {
modules = [
(packagesDirPath + "/${module}")
{
paths.projectRoot = projectRoot;
paths.projectRootFile = projectRootFile;
paths.package = "/${packagesDir}/${module}";
}
];
})
)
(builtins.readDir packagesDirPath);
flake.config.lib.evalModules = args @ {
packageSets,
modules,
# If set, returns the result coming form nixpgs.lib.evalModules as is,
# If set, returns the result coming form nixpkgs.lib.evalModules as is,
# otherwise it returns the derivation only (.config.public).
raw ? false,
specialArgs ? {},
...
}: let
forawardedArgs = builtins.removeAttrs args [
forwardedArgs = builtins.removeAttrs args [
"packageSets"
"raw"
];
@ -24,7 +57,7 @@
evaluated =
lib.evalModules
(
forawardedArgs
forwardedArgs
// {
modules =
args.modules