Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add moduleLocation to mkFlake argument #158

Merged
merged 5 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions dev/tests/eval-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = { }; }
{
Expand Down Expand Up @@ -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";
Expand Down
6 changes: 3 additions & 3 deletions extras/flakeModules.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ self, lib, flake-parts-lib, ... }:
{ self, lib, flake-parts-lib, moduleLocation, ... }:
let
inherit (lib)
mapAttrs
Expand All @@ -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 = ''
Expand Down
26 changes: 19 additions & 7 deletions lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 ];
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions modules/nixosModules.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ self, lib, flake-parts-lib, ... }:
{ self, lib, flake-parts-lib, moduleLocation, ... }:
let
inherit (lib)
mapAttrs
Expand All @@ -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.

Expand Down