From c190b08bb7793004a4706e008a7d0f9b2be3df70 Mon Sep 17 00:00:00 2001 From: matthewcroughan Date: Sat, 9 Apr 2022 01:05:35 +0100 Subject: [PATCH 1/5] lib: add callLocklessFlake This is essentially a copy of the function of the same name, from flake-compat. callLocklessFlake is useful when trying to utilise a flake.nix without a lock file, often for when you want to create a subflake from within a parent flake. Co-authored-by: Tom Bereknyei Co-authored-by: Robert Hensing --- lib/default.nix | 2 +- lib/trivial.nix | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/default.nix b/lib/default.nix index 1f06283790a..7d20acc6794 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -64,7 +64,7 @@ let hasAttr head isAttrs isBool isInt isList isString length lessThan listToAttrs pathExists readFile replaceStrings seq stringLength sub substring tail trace; - inherit (self.trivial) id const pipe concat or and bitAnd bitOr bitXor + inherit (self.trivial) id callLocklessFlake const pipe concat or and bitAnd bitOr bitXor bitNot boolToString mergeAttrs flip mapNullable inNixShell isFloat min max importJSON importTOML warn warnIf warnIfNot throwIf throwIfNot checkListOfEnum info showWarnings nixpkgsVersion version isInOldestRelease diff --git a/lib/trivial.nix b/lib/trivial.nix index 18616a189c2..0b9b6bd910f 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -13,6 +13,22 @@ rec { # The value to return x: x; + /* imports a flake.nix without acknowledging its lock file, useful for + referencing subflakes from a parent flake. The second argument allows + specifying the inputs of this flake. + + Example: + callLocklessFlake { + path = ./directoryContainingFlake; + inputs = { inherit nixpkgs; }; + } + */ + callLocklessFlake = { path, inputs ? {} }: let + self = {outPath = path;} // + ((import (path + "/flake.nix")).outputs (inputs // {self = self;})); + in + self; + /* The constant function Ignores the second argument. If called with only one argument, From cad8bbe58905a14651a469be78f89cad55ee7c15 Mon Sep 17 00:00:00 2001 From: matthewcroughan Date: Tue, 12 Apr 2022 18:55:41 +0100 Subject: [PATCH 2/5] lib: init flakes.nix This commit creates flakes.nix, which is a library containing functions which relate to interacting with flakes. It also moves related functions from trivial.nix into it. --- lib/default.nix | 6 +++++- lib/flakes.nix | 22 ++++++++++++++++++++++ lib/trivial.nix | 16 ---------------- 3 files changed, 27 insertions(+), 17 deletions(-) create mode 100644 lib/flakes.nix diff --git a/lib/default.nix b/lib/default.nix index 7d20acc6794..8d7cc20ae48 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -11,6 +11,9 @@ let callLibs = file: import file { lib = self; }; in { + # interacting with flakes + flakes = callLibs ./flakes.nix; + # often used, or depending on very little trivial = callLibs ./trivial.nix; fixedPoints = callLibs ./fixed-points.nix; @@ -59,12 +62,13 @@ let # linux kernel configuration kernel = callLibs ./kernel.nix; + inherit (self.flakes) callLocklessFlake; inherit (builtins) add addErrorContext attrNames concatLists deepSeq elem elemAt filter genericClosure genList getAttr hasAttr head isAttrs isBool isInt isList isString length lessThan listToAttrs pathExists readFile replaceStrings seq stringLength sub substring tail trace; - inherit (self.trivial) id callLocklessFlake const pipe concat or and bitAnd bitOr bitXor + inherit (self.trivial) id const pipe concat or and bitAnd bitOr bitXor bitNot boolToString mergeAttrs flip mapNullable inNixShell isFloat min max importJSON importTOML warn warnIf warnIfNot throwIf throwIfNot checkListOfEnum info showWarnings nixpkgsVersion version isInOldestRelease diff --git a/lib/flakes.nix b/lib/flakes.nix new file mode 100644 index 00000000000..4dc027b6c9b --- /dev/null +++ b/lib/flakes.nix @@ -0,0 +1,22 @@ +{ lib }: + +rec { + + /* imports a flake.nix without acknowledging its lock file, useful for + referencing subflakes from a parent flake. The second argument allows + specifying the inputs of this flake. + + Example: + callLocklessFlake { + path = ./directoryContainingFlake; + inputs = { inherit nixpkgs; }; + } + */ + callLocklessFlake = { path, inputs ? { } }: + let + self = { outPath = path; } // + ((import (path + "/flake.nix")).outputs (inputs // { self = self; })); + in + self; + +} diff --git a/lib/trivial.nix b/lib/trivial.nix index 0b9b6bd910f..18616a189c2 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -13,22 +13,6 @@ rec { # The value to return x: x; - /* imports a flake.nix without acknowledging its lock file, useful for - referencing subflakes from a parent flake. The second argument allows - specifying the inputs of this flake. - - Example: - callLocklessFlake { - path = ./directoryContainingFlake; - inputs = { inherit nixpkgs; }; - } - */ - callLocklessFlake = { path, inputs ? {} }: let - self = {outPath = path;} // - ((import (path + "/flake.nix")).outputs (inputs // {self = self;})); - in - self; - /* The constant function Ignores the second argument. If called with only one argument, From cc052779fba0d9f0efa5980f42661798598b2ed1 Mon Sep 17 00:00:00 2001 From: matthewcroughan Date: Tue, 12 Apr 2022 19:47:48 +0100 Subject: [PATCH 3/5] lib/tests: add test for callLocklessFlake --- lib/tests/misc.nix | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index fcccf89cc88..9ca1d7ca50c 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -22,6 +22,15 @@ in runTests { +# FLAKES + + testCallLocklessFlake = { + expr = callLocklessFlake { + path = ./..; + inputs = {}; + }; + expected = { lib = import ./..; outPath = ./..; }; + }; # TRIVIAL From ec59145c3b8d69bf1e270fdcfa651348880ad8e4 Mon Sep 17 00:00:00 2001 From: matthewcroughan Date: Tue, 12 Apr 2022 20:38:55 +0100 Subject: [PATCH 4/5] lib/tests: use subflake to test callLocklessFlake --- lib/tests/flakes/subflakeTest/flake.nix | 8 ++++++++ lib/tests/flakes/subflakeTest/subflake/flake.nix | 5 +++++ lib/tests/misc.nix | 6 +++--- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 lib/tests/flakes/subflakeTest/flake.nix create mode 100644 lib/tests/flakes/subflakeTest/subflake/flake.nix diff --git a/lib/tests/flakes/subflakeTest/flake.nix b/lib/tests/flakes/subflakeTest/flake.nix new file mode 100644 index 00000000000..3dca262cace --- /dev/null +++ b/lib/tests/flakes/subflakeTest/flake.nix @@ -0,0 +1,8 @@ +{ + inputs = { + subflake.url = "path:subflake"; + }; + outputs = { self, subflake }: { + x = subflake; + }; +} diff --git a/lib/tests/flakes/subflakeTest/subflake/flake.nix b/lib/tests/flakes/subflakeTest/subflake/flake.nix new file mode 100644 index 00000000000..9ae32d72ef6 --- /dev/null +++ b/lib/tests/flakes/subflakeTest/subflake/flake.nix @@ -0,0 +1,5 @@ +{ + outputs = { self }: { + y = 1; + }; +} diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 9ca1d7ca50c..e11fe648cc9 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -26,10 +26,10 @@ runTests { testCallLocklessFlake = { expr = callLocklessFlake { - path = ./..; - inputs = {}; + path = ./flakes/subflakeTest; + inputs = { subflake = ./flakes/subflakeTest/subflake; }; }; - expected = { lib = import ./..; outPath = ./..; }; + expected = { x = ./flakes/subflakeTest/subflake; outPath = ./flakes/subflakeTest; }; }; # TRIVIAL From 3f128cc0247a95cfdcbb34a96c230422706b1715 Mon Sep 17 00:00:00 2001 From: matthewcroughan Date: Tue, 12 Apr 2022 21:13:26 +0100 Subject: [PATCH 5/5] lib/tests: evaluate value from subflake with callLocklessFlake --- lib/tests/flakes/subflakeTest/flake.nix | 10 +++++----- lib/tests/flakes/subflakeTest/subflake/flake.nix | 2 +- lib/tests/misc.nix | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/tests/flakes/subflakeTest/flake.nix b/lib/tests/flakes/subflakeTest/flake.nix index 3dca262cace..3a8edd5e8c5 100644 --- a/lib/tests/flakes/subflakeTest/flake.nix +++ b/lib/tests/flakes/subflakeTest/flake.nix @@ -1,8 +1,8 @@ { - inputs = { - subflake.url = "path:subflake"; - }; - outputs = { self, subflake }: { - x = subflake; + outputs = { self, subflake, callLocklessFlake }: rec { + x = (callLocklessFlake { + path = subflake; + inputs = {}; + }).subflakeOutput; }; } diff --git a/lib/tests/flakes/subflakeTest/subflake/flake.nix b/lib/tests/flakes/subflakeTest/subflake/flake.nix index 9ae32d72ef6..41566b52090 100644 --- a/lib/tests/flakes/subflakeTest/subflake/flake.nix +++ b/lib/tests/flakes/subflakeTest/subflake/flake.nix @@ -1,5 +1,5 @@ { outputs = { self }: { - y = 1; + subflakeOutput = 1; }; } diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index e11fe648cc9..dcb56d0c1fc 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -27,9 +27,9 @@ runTests { testCallLocklessFlake = { expr = callLocklessFlake { path = ./flakes/subflakeTest; - inputs = { subflake = ./flakes/subflakeTest/subflake; }; + inputs = { subflake = ./flakes/subflakeTest/subflake; inherit callLocklessFlake; }; }; - expected = { x = ./flakes/subflakeTest/subflake; outPath = ./flakes/subflakeTest; }; + expected = { x = 1; outPath = ./flakes/subflakeTest; }; }; # TRIVIAL