feat(paths): allow paths.package to be an absolute path

This commit is contained in:
DavHau 2023-09-20 21:48:16 +02:00
parent 8576970852
commit 4eb5478fdd
8 changed files with 129 additions and 13 deletions

1
ci.nix
View File

@ -7,6 +7,7 @@ let
dream2nix-repo = import ./examples/dream2nix-repo { dream2nix-repo = import ./examples/dream2nix-repo {
dream2nixSource = ./.; dream2nixSource = ./.;
inherit pkgs;
}; };
dream2nix-repo-flake = (import ./examples/dream2nix-repo-flake/flake.nix).outputs { dream2nix-repo-flake = (import ./examples/dream2nix-repo-flake/flake.nix).outputs {

View File

@ -19,7 +19,7 @@
projectRoot = ./.; projectRoot = ./.;
# can be changed to ".git" or "flake.nix" to get rid of .project-root # can be changed to ".git" or "flake.nix" to get rid of .project-root
projectRootFile = "flake.nix"; projectRootFile = "flake.nix";
packagesDir = "/packages"; packagesDir = ./packages;
packageSets.nixpkgs = nixpkgs.legacyPackages.${system}; packageSets.nixpkgs = nixpkgs.legacyPackages.${system};
}; };
}; };

View File

@ -4,16 +4,16 @@
url = "https://github.com/nix-community/dream2nix/tarball/main"; url = "https://github.com/nix-community/dream2nix/tarball/main";
# sha256 = ""; # sha256 = "";
}, },
pkgs ? import (import dream2nixSource).inputs.nixpkgs {},
}: let }: let
dream2nix = import dream2nixSource; dream2nix = import dream2nixSource;
nixpkgs = import dream2nix.inputs.nixpkgs {};
# all packages defined inside ./packages/ # all packages defined inside ./packages/
packages = dream2nix.lib.importPackages { packages = dream2nix.lib.importPackages {
projectRoot = ./.; projectRoot = ./.;
# can be changed to ".git" to get rid of .project-root # can be changed to ".git" to get rid of .project-root
projectRootFile = ".project-root"; projectRootFile = ".project-root";
packagesDir = "/packages"; packagesDir = ./packages;
packageSets.nixpkgs = nixpkgs; packageSets.nixpkgs = pkgs;
}; };
in in
# all packages defined inside ./packages/ # all packages defined inside ./packages/

View File

@ -2,11 +2,13 @@
lib, lib,
config, config,
... ...
}: { }: let
cfg = config.paths;
in {
options.paths = lib.mapAttrs (_: lib.mkOption) { options.paths = lib.mapAttrs (_: lib.mkOption) {
# mandatory fields # mandatory fields
projectRoot = { projectRoot = {
type = lib.types.path; type = lib.types.pathInStore;
description = '' description = ''
Path to the root of the project on which dream2nix operates. Path to the root of the project on which dream2nix operates.
Must contain the marker file specified by 'paths.projectRootFile' Must contain the marker file specified by 'paths.projectRootFile'
@ -16,13 +18,22 @@
example = lib.literalExpression "./."; example = lib.literalExpression "./.";
}; };
package = { package = {
type = lib.types.str; type = lib.types.either lib.types.path lib.types.str;
description = '' description = ''
Path to the directory containing the definition of the current package. Path to the directory containing the definition of the current package.
Relative to 'paths.projectRoot'. Relative to 'paths.projectRoot'.
This helps locating package definitions for lock & update scripts. This helps locating package definitions for lock & update scripts.
''; '';
apply = path': let
projectRoot = toString cfg.projectRoot;
path = toString path';
in
if path == projectRoot
then "./."
else if lib.hasPrefix projectRoot path
then lib.path.subpath.normalise "./${lib.removePrefix projectRoot path}"
else lib.path.subpath.normalise "./${path}";
}; };
# optional fields # optional fields

View File

@ -20,7 +20,7 @@ in {
})); }));
}; };
composerInstallFlags = { composerInstallFlags = {
type = t.listOf t.string; type = t.listOf t.str;
default = []; default = [];
}; };
}; };

View File

@ -30,6 +30,15 @@
packagePath = "/examples/packages/${dirName}/${name}"; packagePath = "/examples/packages/${dirName}/${name}";
}); });
importFlake = flakeFile: let
self' = (import flakeFile).outputs {
dream2nix = self;
nixpkgs = inputs.nixpkgs;
self = self';
};
in
self';
# Type: [ {${name} = {module, packagePath} ] # Type: [ {${name} = {module, packagePath} ]
allExamples = mapAttrsToList (dirName: _: readExamples dirName) packageCategories; allExamples = mapAttrsToList (dirName: _: readExamples dirName) packageCategories;
@ -96,7 +105,16 @@ in {
in { in {
# map all modules in /examples to a package output in the flake. # map all modules in /examples to a package output in the flake.
checks = checks =
lib.mapAttrs (_: drvModules: makeDrv drvModules) (lib.mapAttrs (_: drvModules: makeDrv drvModules) allModules)
allModules; // {
example-repo =
(import (self + /examples/dream2nix-repo) {
dream2nixSource = self;
inherit pkgs;
})
.hello;
example-repo-flake =
(importFlake (self + /examples/dream2nix-repo-flake/flake.nix)).packages.${system}.hello;
};
}; };
} }

View File

@ -13,9 +13,11 @@
packagesDir, packagesDir,
... ...
}: let }: let
projectRoot = toString args.projectRoot;
packagesDir = toString args.packagesDir;
packagesDirPath = packagesDirPath =
if ! builtins.isString packagesDir if lib.hasPrefix projectRoot packagesDir
then throw "packagesDir must be a string" then packagesDir
else projectRoot + "/${packagesDir}"; else projectRoot + "/${packagesDir}";
forwardedArgs = builtins.removeAttrs args [ forwardedArgs = builtins.removeAttrs args [
"projectRoot" "projectRoot"
@ -36,7 +38,7 @@
{ {
paths.projectRoot = projectRoot; paths.projectRoot = projectRoot;
paths.projectRootFile = projectRootFile; paths.projectRootFile = projectRootFile;
paths.package = "/${packagesDir}/${module}"; paths.package = packagesDir + "/${module}";
} }
]; ];
}) })

View File

@ -0,0 +1,84 @@
{
pkgs ? import <nixpkgs> {},
lib ? import <nixpkgs/lib>,
dream2nix ? (import (../../../modules + "/flake.nix")).outputs {},
}: let
eval = module:
(lib.evalModules {
modules = [
dream2nix.modules.dream2nix.core
module
];
specialArgs = {
inherit dream2nix;
packageSets.nixpkgs = pkgs;
};
})
.config;
in {
test_package_in_root_1 = let
config = eval {
paths.projectRoot = "${./.}";
paths.package = "${./.}";
};
in {
expr = {inherit (config.paths) projectRoot package;};
expected = {
projectRoot = "${./.}";
package = "./.";
};
};
test_package_in_root_2 = let
config = eval {
paths.projectRoot = "${./.}";
paths.package = "./.";
};
in {
expr = {inherit (config.paths) projectRoot package;};
expected = {
projectRoot = "${./.}";
package = "./.";
};
};
test_package_in_subdir_1 = let
config = eval {
paths.projectRoot = "${./.}";
paths.package = "${./.}/package";
};
in {
expr = {inherit (config.paths) projectRoot package;};
expected = {
projectRoot = "${./.}";
package = "./package";
};
};
test_package_in_subdir_2 = let
config = eval {
paths.projectRoot = "${./.}";
paths.package = "./package";
};
in {
expr = {inherit (config.paths) projectRoot package;};
expected = {
projectRoot = "${./.}";
package = "./package";
};
};
test_package_in_subdir_3 = let
self = /nix/store/some/path;
config = eval {
paths.projectRoot = self;
paths.package = self + /package;
};
in {
expr = {inherit (config.paths) projectRoot package;};
expected = {
projectRoot = self;
package = "./package";
};
};
}