aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-14 03:45:10 +0000
committertv <tv@krebsco.de>2026-11-14 04:58:40 +0100
commit4ac279ac50b2a842b08d39a875c7c1b89998fd13 (patch)
tree6a614a9b9b9dc47ace1292100ca01c87e9f97098
parent3e2116f153077186556c01ae1fc89923abfea265 (diff)
krops: take nixpkgs' lib from the caller
krops' lib imported <nixpkgs/lib>, which pure evaluation doesn't allow, and so did pkgs.krops and pkgs.populate, through it, also in krops' flake. lib/with.nix is krops' own lib, a function taking nixpkgs' lib, without merging it in. pkgs.krops and pkgs.populate use it with the lib callPackage gives them, nixpkgs.lib in krops' flake, and lib/default.nix is what it was: <nixpkgs/lib>, builtins, and krops' own lib, merged. The derivations stay the same.
-rw-r--r--CHANGELOG.md4
-rw-r--r--README.md14
-rw-r--r--lib/default.nix117
-rw-r--r--lib/with.nix110
-rw-r--r--pkgs/krops/checks.nix8
-rw-r--r--pkgs/krops/default.nix30
-rw-r--r--pkgs/krops/populate.nix12
-rw-r--r--tests/default.nix2
8 files changed, 158 insertions, 139 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bcc74ae..642c8a6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,10 @@
each set of arguments. `pkgs.populate-unwrapped` is the program;
`pkgs.populate`, `writeDeploy`, and the other functions keep their
arguments, and `pkgs.populate` also takes a target string.
+- `lib/with.nix` is krops' lib without nixpkgs' lib, which it takes from
+ its caller, e.g. a flake's `nixpkgs.lib`. `pkgs.krops`, `pkgs.populate`
+ and krops' flake use the nixpkgs lib they get, instead of
+ `<nixpkgs/lib>`, which `lib/default.nix` still merges with krops' lib.
- A `passage` source's `identities_file` defaults to
`~/.passage/identities` when populate runs, instead of during
evaluation.
diff --git a/README.md b/README.md
index 92dab4a..d71a6d0 100644
--- a/README.md
+++ b/README.md
@@ -75,7 +75,9 @@ machine that uses them.
krops' flake provides `populate`, `writeDeploy`, and the other functions
described below as `lib.<system>`, and its `lib` directory provides
-`evalSource` and `mkTarget`:
+`evalSource` and `mkTarget`. `lib/with.nix` is a function taking nixpkgs'
+lib, as below; `lib/default.nix` merges it, with `<nixpkgs/lib>`, into
+nixpkgs' lib and builtins:
```nix
{
@@ -83,9 +85,9 @@ described below as `lib.<system>`, and its `lib` directory provides
inputs.krops.url = "git+https://cgit.krebsco.de/krops";
inputs.krops.inputs.nixpkgs.follows = "nixpkgs";
- outputs = { self, krops, ... }: let
+ outputs = { self, krops, nixpkgs, ... }: let
system = "x86_64-linux";
- lib = import "${krops}/lib";
+ lib = import "${krops}/lib/with.nix" { inherit (nixpkgs) lib; };
in {
apps.${system}.deploy = {
type = "app";
@@ -101,8 +103,8 @@ described below as `lib.<system>`, and its `lib` directory provides
}
```
-krops evaluates `<nixpkgs/lib>`, environment variables, and `/etc/hostname`,
-which pure evaluation doesn't allow, so deploy with `nix run --impure .#deploy`.
+krops evaluates environment variables and `/etc/hostname`, which pure
+evaluation doesn't allow, so deploy with `nix run --impure .#deploy`.
`pkgs.krops` provides the following functions.
@@ -150,7 +152,7 @@ pkgs.krops.writeDeploy "deploy" {
}
```
For more details about the `target` attribute, please check the `mkTarget`
-function in [lib/default.nix](lib/default.nix).
+function in [lib/with.nix](lib/with.nix).
### `allocateTTY` (optional, defaults to false)
diff --git a/lib/default.nix b/lib/default.nix
index 4eb6438..89788e4 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -1,111 +1,6 @@
-let {
-
- body = lib;
-
- lib = nixpkgs.lib // builtins // {
-
- # What populate's Nix library defines.
- inherit (populate.lib) evalSource flakeFollows hasGeneratedFlake derivesFlake;
-
- maybeHostName = default: let
- # We're parsing /etc/hostname here because reading
- # /proc/sys/kernel/hostname yields ""
- path = "/etc/hostname";
- lines = lib.splitString "\n" (lib.readFile path);
- hostNames = lib.filter lib.types.label.check lines;
- in
- if lib.pathExists path then
- if lib.length hostNames == 1 then
- lib.head hostNames
- else
- lib.trace "malformed ${path}" default
- else
- default;
-
- firstWord = s:
- lib.head (lib.match "^([^[:space:]]*).*" s);
-
- isLocalTarget = let
- origin = lib.mkTarget "";
- in target:
- target.user == origin.user &&
- lib.elem target.host [origin.host "localhost"];
-
- mkTarget = s: let
- parse = lib.match "(([^@]*)@)?(([^:/]+))?(:([^/]+))?(/.*)?" s;
- elemAt' = xs: i: if lib.length xs > i then lib.elemAt xs i else null;
- filterNull = lib.filterAttrs (n: v: v != null);
- in {
- user = lib.maybeEnv "LOGNAME" null;
- host = lib.maybeEnv "HOSTNAME" (lib.maybeHostName "localhost");
- port = null;
- path = "/var/src";
- sudo = false;
- extraOptions = [];
- } // (if lib.isString s then filterNull {
- user = elemAt' parse 1;
- host = elemAt' parse 3;
- port = elemAt' parse 5;
- path = elemAt' parse 6;
- } else s);
-
- mkUserPortSSHOpts = target:
- (lib.optionals (target.user != null) ["-l" target.user]) ++
- (lib.optionals (target.port != null) ["-p" target.port]);
-
- shell = let
- isSafeChar = lib.testString "[-+./0-9:=A-Z_a-z]";
- quoteChar = c:
- if isSafeChar c then c
- else if c == "\n" then "'\n'"
- else "\\${c}";
- in {
- quote = x: if x == "" then "''" else lib.stringAsChars quoteChar x;
-
- # Shell code defining the function url_path, which prints the path $1
- # as it goes into a URL: with every byte percent-encoded that isn't a
- # letter, a digit, or one of -._~/. Nix decodes that, and otherwise
- # takes, e.g., ? and # for delimiters, and refuses blanks.
- urlPath = /* sh */ ''
- url_path() {
- printf %s "$1" | od -An -v -tx1 | tr -s ' ' '\n' |
- while read -r url_byte; do
- case $url_byte in
- "") ;;
- 2[d-f]|3[0-9]|4[1-9a-f]|5[0-9a]|5f|6[1-9a-f]|7[0-9a]|7e)
- printf "\\$(printf %o "0x$url_byte")"
- ;;
- *)
- printf '%%%s' "$url_byte"
- ;;
- esac
- done
- }
- '';
-
- # Shell code that enables flakes for the Nix commands following it, and
- # fails unless Nix supports them, i.e. is version 2.4 or later. `what`
- # names what needs flakes, for the error message.
- requireFlakes = what: /* sh */ ''
- NIX_CONFIG=$(printf '%s\n%s' "''${NIX_CONFIG-}" \
- 'extra-experimental-features = nix-command flakes')
- export NIX_CONFIG
- if ! nix flake --help >/dev/null 2>&1; then
- >&2 printf 'error: %s needs Nix 2.4 or later on %s, found: %s\n' \
- ${lib.shell.quote what} "$(uname -n)" "$(nix --version 2>&1 || :)"
- exit 1
- fi
- '';
- };
-
- test = re: x: lib.isString x && lib.testString re x;
- testString = re: x: lib.match re x != null;
-
- types = nixpkgs.lib.types // populate.lib.types;
- };
-
- nixpkgs.lib = import <nixpkgs/lib>;
-
- populate.lib = import ../pkgs/populate/lib { lib = nixpkgs.lib; };
-
-}
+# krops' lib: nixpkgs' lib from <nixpkgs/lib>, builtins, and what krops
+# defines, see ./with.nix, which takes nixpkgs' lib from its caller instead.
+let
+ lib = import <nixpkgs/lib>;
+in
+ lib // builtins // import ./with.nix { inherit lib; }
diff --git a/lib/with.nix b/lib/with.nix
new file mode 100644
index 0000000..7305b7d
--- /dev/null
+++ b/lib/with.nix
@@ -0,0 +1,110 @@
+# krops' own lib, without nixpkgs' lib, which it takes as lib, e.g. a
+# flake's nixpkgs.lib. ./default.nix is nixpkgs' lib from <nixpkgs/lib>,
+# builtins, and this, as one.
+{ lib }:
+
+let
+ populate.lib = import ../pkgs/populate/lib { inherit lib; };
+in
+
+rec {
+
+ # What populate's Nix library defines.
+ inherit (populate.lib) evalSource flakeFollows hasGeneratedFlake derivesFlake;
+
+ maybeHostName = default: let
+ # We're parsing /etc/hostname here because reading
+ # /proc/sys/kernel/hostname yields ""
+ path = "/etc/hostname";
+ lines = lib.splitString "\n" (lib.readFile path);
+ hostNames = lib.filter types.label.check lines;
+ in
+ if lib.pathExists path then
+ if lib.length hostNames == 1 then
+ lib.head hostNames
+ else
+ lib.trace "malformed ${path}" default
+ else
+ default;
+
+ firstWord = s:
+ lib.head (lib.match "^([^[:space:]]*).*" s);
+
+ isLocalTarget = let
+ origin = mkTarget "";
+ in target:
+ target.user == origin.user &&
+ lib.elem target.host [origin.host "localhost"];
+
+ mkTarget = s: let
+ parse = lib.match "(([^@]*)@)?(([^:/]+))?(:([^/]+))?(/.*)?" s;
+ elemAt' = xs: i: if lib.length xs > i then lib.elemAt xs i else null;
+ filterNull = lib.filterAttrs (n: v: v != null);
+ in {
+ user = lib.maybeEnv "LOGNAME" null;
+ host = lib.maybeEnv "HOSTNAME" (maybeHostName "localhost");
+ port = null;
+ path = "/var/src";
+ sudo = false;
+ extraOptions = [];
+ } // (if lib.isString s then filterNull {
+ user = elemAt' parse 1;
+ host = elemAt' parse 3;
+ port = elemAt' parse 5;
+ path = elemAt' parse 6;
+ } else s);
+
+ mkUserPortSSHOpts = target:
+ (lib.optionals (target.user != null) ["-l" target.user]) ++
+ (lib.optionals (target.port != null) ["-p" target.port]);
+
+ shell = let
+ isSafeChar = testString "[-+./0-9:=A-Z_a-z]";
+ quoteChar = c:
+ if isSafeChar c then c
+ else if c == "\n" then "'\n'"
+ else "\\${c}";
+ in {
+ quote = x: if x == "" then "''" else lib.stringAsChars quoteChar x;
+
+ # Shell code defining the function url_path, which prints the path $1
+ # as it goes into a URL: with every byte percent-encoded that isn't a
+ # letter, a digit, or one of -._~/. Nix decodes that, and otherwise
+ # takes, e.g., ? and # for delimiters, and refuses blanks.
+ urlPath = /* sh */ ''
+ url_path() {
+ printf %s "$1" | od -An -v -tx1 | tr -s ' ' '\n' |
+ while read -r url_byte; do
+ case $url_byte in
+ "") ;;
+ 2[d-f]|3[0-9]|4[1-9a-f]|5[0-9a]|5f|6[1-9a-f]|7[0-9a]|7e)
+ printf "\\$(printf %o "0x$url_byte")"
+ ;;
+ *)
+ printf '%%%s' "$url_byte"
+ ;;
+ esac
+ done
+ }
+ '';
+
+ # Shell code that enables flakes for the Nix commands following it, and
+ # fails unless Nix supports them, i.e. is version 2.4 or later. `what`
+ # names what needs flakes, for the error message.
+ requireFlakes = what: /* sh */ ''
+ NIX_CONFIG=$(printf '%s\n%s' "''${NIX_CONFIG-}" \
+ 'extra-experimental-features = nix-command flakes')
+ export NIX_CONFIG
+ if ! nix flake --help >/dev/null 2>&1; then
+ >&2 printf 'error: %s needs Nix 2.4 or later on %s, found: %s\n' \
+ ${shell.quote what} "$(uname -n)" "$(nix --version 2>&1 || :)"
+ exit 1
+ fi
+ '';
+ };
+
+ test = re: x: lib.isString x && testString re x;
+ testString = re: x: lib.match re x != null;
+
+ types = lib.types // populate.lib.types;
+}
diff --git a/pkgs/krops/checks.nix b/pkgs/krops/checks.nix
index 22b45c9..c6e0da7 100644
--- a/pkgs/krops/checks.nix
+++ b/pkgs/krops/checks.nix
@@ -1,6 +1,10 @@
# Checks of populate's arguments during evaluation. Each returns true, or
-# throws an error naming what's wrong.
-with import ../../lib;
+# throws an error naming what's wrong. lib is nixpkgs' lib.
+{ lib }:
+
+with lib;
+with builtins;
+with import ../../lib/with.nix { inherit lib; };
{
# Populate replaces and removes what's at the target paths of sources, so
diff --git a/pkgs/krops/default.nix b/pkgs/krops/default.nix
index d33bcb5..248f033 100644
--- a/pkgs/krops/default.nix
+++ b/pkgs/krops/default.nix
@@ -1,8 +1,10 @@
+{ expect, lib, nix, openssh, populate, writers }:
+
let
- lib = import ../../lib;
+ klib = import ../../lib/with.nix { inherit lib; };
in
-{ expect, nix, openssh, populate, writers }: rec {
+rec {
rebuild = {
allocateTTY ? false,
@@ -18,8 +20,8 @@ in
runShell target { inherit allocateTTY; }
(withNixOutputMonitor target useNixOutputMonitor /* sh */ ''
${lib.optionalString (flake != null) /* sh */ ''
- ${lib.shell.requireFlakes "deploying a flake"}
- ${lib.shell.urlPath}
+ ${klib.shell.requireFlakes "deploying a flake"}
+ ${klib.shell.urlPath}
# A Git work tree gets deployed as such. Anything else gets
# deployed as path:, not just by its path, which Nix takes for the
# enclosing Git work tree, if any. Nix refuses URLs with symlinks
@@ -36,7 +38,7 @@ in
fi
''}
NIX_PATH=${lib.escapeShellArg target.path} \
- ${lib.optionalString (allocateTTY && lib.isLocalTarget target)
+ ${lib.optionalString (allocateTTY && klib.isLocalTarget target)
# Without ssh, which allocates the terminal on remote targets.
"${expect}/bin/unbuffer"} \
nixos-rebuild ${lib.escapeShellArgs args} ${
@@ -58,12 +60,12 @@ in
/bin/sh -c ${lib.escapeShellArg command}
'';
in
- if lib.isLocalTarget target
+ if klib.isLocalTarget target
then command'
else
- writers.writeDash "krops.${target.host}.${lib.firstWord command}" ''
+ writers.writeDash "krops.${target.host}.${klib.firstWord command}" ''
exec ${openssh}/bin/ssh ${lib.escapeShellArgs (lib.flatten [
- (lib.mkUserPortSSHOpts target)
+ (klib.mkUserPortSSHOpts target)
(if allocateTTY then "-t" else "-T")
target.extraOptions
target.host
@@ -114,7 +116,7 @@ in
source,
target
}: let
- target' = lib.mkTarget target;
+ target' = klib.mkTarget target;
in
writers.writeDash name ''
set -efu
@@ -139,8 +141,8 @@ in
buildTarget' =
if buildTarget == null
then target'
- else lib.mkTarget buildTarget;
- target' = lib.mkTarget target;
+ else klib.mkTarget buildTarget;
+ target' = klib.mkTarget target;
# A host as nixos-rebuild takes it. Without a user, ssh decides.
userHost = target:
lib.optionalString (target.user != null && target.user != "")
@@ -153,7 +155,7 @@ in
if flake == null then null
else if !(source ? ${name}) then
throw "krops: flake ${name} is not a source"
- else if lib.hasGeneratedFlake source.${name} then
+ else if klib.hasGeneratedFlake source.${name} then
".populate/flakes/${flake}"
else
flake;
@@ -186,9 +188,9 @@ in
target,
trace ? false
}: let
- target' = lib.mkTarget target;
+ target' = klib.mkTarget target;
in
- assert lib.isLocalTarget target';
+ assert klib.isLocalTarget target';
writers.writeDash name ''
set -efu
${populate { inherit backup force prune source; target = target'; }} >&2
diff --git a/pkgs/krops/populate.nix b/pkgs/krops/populate.nix
index 74ba04a..c887e04 100644
--- a/pkgs/krops/populate.nix
+++ b/pkgs/krops/populate.nix
@@ -1,22 +1,24 @@
# pkgs.populate: given populate's arguments, it checks them, and returns a
# script running populate-unwrapped, the program in ../populate, with them,
# the sources as JSON.
-{ coreutils, findutils, git, nix, openssh, pass, passage, rsync, writers
+{ coreutils, findutils, git, lib, nix, openssh, pass, passage, rsync, writers
, populate-unwrapped
, ...
}@args:
-with import ../../lib;
+with lib;
+with builtins;
+with import ../../lib/with.nix { inherit lib; };
let
- checks = import ./checks.nix;
+ checks = import ./checks.nix { inherit lib; };
# Arguments populate doesn't use, which overrides written for krops 1 may
# set. They come through ..., because callPackage would set named ones.
unused = filter (name: args ? ${name}) [ "dash" "implementation" "jq" ];
unexpected = attrNames (removeAttrs args ([
- "coreutils" "findutils" "git" "nix" "openssh" "pass" "passage" "rsync"
- "writers" "populate-unwrapped"
+ "coreutils" "findutils" "git" "lib" "nix" "openssh" "pass" "passage"
+ "rsync" "writers" "populate-unwrapped"
] ++ unused));
# The parts of a target string, like mkTarget parses it, without its
diff --git a/tests/default.nix b/tests/default.nix
index 475ab5d..fa82e3a 100644
--- a/tests/default.nix
+++ b/tests/default.nix
@@ -18,7 +18,7 @@
let
inherit (pkgs) lib;
- krops = import ../lib;
+ krops = import ../lib/with.nix { inherit lib; };
populateTests = ../pkgs/populate/tests;
inherit (import (populateTests + "/fixtures.nix")) gitHead gitFirst;