1
0
Fork 0
mirror of https://github.com/hercules-ci/flake-parts.git synced 2024-12-14 11:47:31 +00:00

Merge pull request #192 from hercules-ci/moduleLocation

moduleLocation, but also automatic
This commit is contained in:
Robert Hensing 2023-10-29 12:23:28 +01:00 committed by GitHub
commit 25abf6caa8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 12 deletions

View file

@ -26,6 +26,22 @@ rec {
systems = [ ];
};
emptyExposeArgs = mkFlake
{ inputs.self = { outPath = "the self outpath"; }; }
({ config, moduleLocation, ... }: {
flake = {
inherit moduleLocation;
};
});
emptyExposeArgsNoSelf = mkFlake
{ inputs.self = throw "self won't be available in case of some errors"; }
({ config, moduleLocation, ... }: {
flake = {
inherit moduleLocation;
};
});
example1 = mkFlake
{ inputs.self = { }; }
{
@ -162,6 +178,8 @@ rec {
assert packagesNonStrictInDevShells.packages.a.default == pkg "a" "hello";
assert emptyExposeArgs.moduleLocation == "the self outpath/flake.nix";
ok;
result = runTests "ok";

View file

@ -1,4 +1,4 @@
{ self, lib, flake-parts-lib, ... }:
{ self, lib, flake-parts-lib, moduleLocation, ... }:
let
inherit (lib)
mapAttrs
@ -13,8 +13,8 @@ let
type = types.lazyAttrsOf types.deferredModule;
default = { };
apply = mapAttrs (k: v: {
_file = "${toString self.outPath}/flake.nix#flakeModules.${k}";
key = "${toString self.outPath}/flake.nix#flakeModules.${k}";
_file = "${toString moduleLocation}#flakeModules.${k}";
key = "${toString moduleLocation}#flakeModules.${k}";
imports = [ v ];
});
description = ''

26
lib.nix
View file

@ -81,7 +81,24 @@ let
${errorExample}
'')
, moduleLocation ? "${self.outPath}/flake.nix"
}:
let
module = lib.setDefaultModuleLocation errorLocation module;
inputsPos = builtins.unsafeGetAttrPos "inputs" args;
errorLocation =
# Best case: user makes it explicit
args.moduleLocation or (
# Slightly worse: Nix does not technically commit to unsafeGetAttrPos semantics
if inputsPos != null
then inputsPos.file
# Slightly worse: self may not be valid when an error occurs
else if args?inputs.self.outPath
then args.inputs.self.outPath + "/flake.nix"
# Fallback
else "<mkFlake argument>"
);
in
throwIf
(!args?self && !args?inputs) ''
When invoking flake-parts, you must pass in the flake output arguments.
@ -100,7 +117,7 @@ let
(module:
lib.evalModules {
specialArgs = {
inherit self flake-parts-lib;
inherit self flake-parts-lib moduleLocation;
inputs = args.inputs or /* legacy, warned above */ self.inputs;
} // specialArgs;
modules = [ ./all-modules.nix module ];
@ -120,12 +137,7 @@ let
mkFlake = args: module:
let
loc =
if args?inputs.self.outPath
then args.inputs.self.outPath + "/flake.nix"
else "<mkFlake argument>";
mod = lib.setDefaultModuleLocation loc module;
eval = flake-parts-lib.evalFlakeModule args mod;
eval = flake-parts-lib.evalFlakeModule args module;
in
eval.config.flake;

View file

@ -1,4 +1,4 @@
{ self, lib, flake-parts-lib, ... }:
{ self, lib, flake-parts-lib, moduleLocation, ... }:
let
inherit (lib)
mapAttrs
@ -15,7 +15,7 @@ in
nixosModules = mkOption {
type = types.lazyAttrsOf types.unspecified;
default = { };
apply = mapAttrs (k: v: { _file = "${toString self.outPath}/flake.nix#nixosModules.${k}"; imports = [ v ]; });
apply = mapAttrs (k: v: { _file = "${toString moduleLocation}#nixosModules.${k}"; imports = [ v ]; });
description = ''
NixOS modules.