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 {
dream2nixSource = ./.;
inherit pkgs;
};
dream2nix-repo-flake = (import ./examples/dream2nix-repo-flake/flake.nix).outputs {

View File

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

View File

@ -4,16 +4,16 @@
url = "https://github.com/nix-community/dream2nix/tarball/main";
# sha256 = "";
},
pkgs ? import (import dream2nixSource).inputs.nixpkgs {},
}: let
dream2nix = import dream2nixSource;
nixpkgs = import dream2nix.inputs.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;
packagesDir = ./packages;
packageSets.nixpkgs = pkgs;
};
in
# all packages defined inside ./packages/

View File

@ -2,11 +2,13 @@
lib,
config,
...
}: {
}: let
cfg = config.paths;
in {
options.paths = lib.mapAttrs (_: lib.mkOption) {
# mandatory fields
projectRoot = {
type = lib.types.path;
type = lib.types.pathInStore;
description = ''
Path to the root of the project on which dream2nix operates.
Must contain the marker file specified by 'paths.projectRootFile'
@ -16,13 +18,22 @@
example = lib.literalExpression "./.";
};
package = {
type = lib.types.str;
type = lib.types.either lib.types.path lib.types.str;
description = ''
Path to the directory containing the definition of the current package.
Relative to 'paths.projectRoot'.
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

View File

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

View File

@ -30,6 +30,15 @@
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} ]
allExamples = mapAttrsToList (dirName: _: readExamples dirName) packageCategories;
@ -96,7 +105,16 @@ in {
in {
# map all modules in /examples to a package output in the flake.
checks =
lib.mapAttrs (_: drvModules: makeDrv drvModules)
allModules;
(lib.mapAttrs (_: drvModules: makeDrv drvModules) 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,
...
}: let
projectRoot = toString args.projectRoot;
packagesDir = toString args.packagesDir;
packagesDirPath =
if ! builtins.isString packagesDir
then throw "packagesDir must be a string"
if lib.hasPrefix projectRoot packagesDir
then packagesDir
else projectRoot + "/${packagesDir}";
forwardedArgs = builtins.removeAttrs args [
"projectRoot"
@ -36,7 +38,7 @@
{
paths.projectRoot = projectRoot;
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";
};
};
}