Repository: nix-darwin/nix-darwin
Pull request #1432: nix-tools: use replaceVarsWith
Author: login: WeetHet
Created: 2025-05-01T14:10:11Z | Merged: 2025-05-03T06:49:03Z

Closes #1426 

Linked issue #1426: substituteAll is deprecated and should be replaced with replaceVars
Currently, `substituteAll` is used in 

https://github.com/nix-darwin/nix-darwin/blob/4515dacafb0ccd42e5395aacc49fd58a43027e01/pkgs/nix-tools/default.nix#L1-L10

This results in evaluation warnings. The usages should be replaced with `replaceVars`
Linked issue comments: user: login: Enzime
created_at: 2025-04-30T12:04:18Z
body: If you could make a PR that would be great 👍
author_association: MEMBER

Diff:
diff --git a/flake.lock b/flake.lock
index 0f05e93e0..3a2b94b3c 100644
--- a/flake.lock
+++ b/flake.lock
@@ -2,11 +2,11 @@
   "nodes": {
     "nixpkgs": {
       "locked": {
-        "lastModified": 1736241350,
-        "narHash": "sha256-CHd7yhaDigUuJyDeX0SADbTM9FXfiWaeNyY34FL1wQU=",
+        "lastModified": 1746061036,
+        "narHash": "sha256-OxYwCGJf9VJ2KnUO+w/hVJVTjOgscdDg/lPv8Eus07Y=",
         "owner": "NixOS",
         "repo": "nixpkgs",
-        "rev": "8c9fd3e564728e90829ee7dbac6edc972971cd0f",
+        "rev": "3afd19146cac33ed242fc0fc87481c67c758a59e",
         "type": "github"
       },
       "original": {
diff --git a/pkgs/nix-tools/default.nix b/pkgs/nix-tools/default.nix
index a5414cbdb..7351ddaeb 100644
--- a/pkgs/nix-tools/default.nix
+++ b/pkgs/nix-tools/default.nix
@@ -2,7 +2,7 @@
 , coreutils
 , jq
 , git
-, substituteAll
+, replaceVarsWith
 , stdenv
 , profile ? "/nix/var/nix/profiles/system"
 , # This should be kept in sync with the default
@@ -31,40 +31,53 @@
 
 let
   extraPath = lib.makeBinPath [ coreutils jq git nixPackage ];
-
-  writeProgram = name: env: src:
-    substituteAll ({
-      inherit name src;
-      dir = "bin";
-      isExecutable = true;
-      meta.mainProgram = name;
-    } // env);
+  
+  writeProgram =
+    attrs:
+    replaceVarsWith (
+      attrs
+      // {
+        dir = "bin";
+        isExecutable = true;
+        meta.mainProgram = attrs.name;
+      }
+    );
 
   path = "${extraPath}:${systemPath}";
 in
 {
-  darwin-option = writeProgram "darwin-option"
-    {
+  darwin-option = writeProgram {
+    name = "darwin-option";
+    src = ./darwin-option.sh;
+
+    replacements = {
       inherit path nixPath;
       inherit (stdenv) shell;
-    }
-    ./darwin-option.sh;
+    };
+  };
+
+  darwin-rebuild = writeProgram {
+    name = "darwin-rebuild";
+    src = ./darwin-rebuild.sh;
 
-  darwin-rebuild = writeProgram "darwin-rebuild"
-    {
+    replacements = {
       inherit path nixPath profile;
       inherit (stdenv) shell;
-      postInstall = ''
-        mkdir -p $out/share/zsh/site-functions
-        cp ${./darwin-rebuild.zsh-completions} $out/share/zsh/site-functions/_darwin-rebuild
-      '';
-    }
-    ./darwin-rebuild.sh;
+    };
+
+    postInstall = ''
+      mkdir -p $out/share/zsh/site-functions
+      cp ${./darwin-rebuild.zsh-completions} $out/share/zsh/site-functions/_darwin-rebuild
+    '';
+  };
+
+  darwin-version = writeProgram {
+    name = "darwin-version";
+    src = ./darwin-version.sh;
 
-  darwin-version = writeProgram "darwin-version"
-    {
+    replacements = {
       inherit (stdenv) shell;
       path = lib.makeBinPath [ jq ];
-    }
-    ./darwin-version.sh;
+    };
+  };
 }


Reviews:
user: login: Enzime
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-01T15:20:32Z
user: login: WeetHet
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-01T15:24:03Z
user: login: Enzime
body: LGTM

Thanks for the contribution
state: APPROVED
author_association: MEMBER
submitted_at: 2025-05-03T06:48:27Z

Review comments:
diff_hunk: @@ -1,70 +1,89 @@
-{ lib
-, coreutils
-, jq
-, git
-, substituteAll
-, stdenv
-, profile ? "/nix/var/nix/profiles/system"
-, # This should be kept in sync with the default
+{
+  lib,
+  coreutils,
+  jq,
+  git,
+  replaceVarsWith,
+  stdenv,
+  profile ? "/nix/var/nix/profiles/system",
+  # This should be kept in sync with the default
   # `environment.systemPath`. We err on side of including conditional
   # things like the profile directories, since they’re more likely to
   # help than hurt, and this default is mostly used for fresh
   # installations anyway.
   systemPath ? lib.concatStringsSep ":" [
-  "$HOME/.nix-profile/bin"
-  "/etc/profiles/per-user/$USER/bin"
-  "/run/current-system/sw/bin"
-  "/nix/var/nix/profiles/default/bin"
-  "/usr/local/bin"
-  "/usr/bin"
-  "/bin"
-  "/usr/sbin"
-  "/sbin"
-]
-, nixPackage ? null
-, # This should be kept in sync with the default `nix.nixPath`.
+    "$HOME/.nix-profile/bin"
+    "/etc/profiles/per-user/$USER/bin"
+    "/run/current-system/sw/bin"
+    "/nix/var/nix/profiles/default/bin"
+    "/usr/local/bin"
+    "/usr/bin"
+    "/bin"
+    "/usr/sbin"
+    "/sbin"
+  ],
+  nixPackage ? null,
+  # This should be kept in sync with the default `nix.nixPath`.
   nixPath ? lib.concatStringsSep ":" [
-  "darwin-config=/etc/nix-darwin/configuration.nix"
-  "/nix/var/nix/profiles/per-user/root/channels"
-]
+    "darwin-config=/etc/nix-darwin/configuration.nix"
+    "/nix/var/nix/profiles/per-user/root/channels"
+  ],
 }:
 
 let
-  extraPath = lib.makeBinPath [ coreutils jq git nixPackage ];
-
-  writeProgram = name: env: src:
-    substituteAll ({
-      inherit name src;
-      dir = "bin";
-      isExecutable = true;
-      meta.mainProgram = name;
-    } // env);
+  extraPath = lib.makeBinPath [
+    coreutils
+    jq
+    git
+    nixPackage
+  ];
 
   path = "${extraPath}:${systemPath}";
+
+  writeProgram =
+    attrs:
+    replaceVarsWith (
+      attrs
+      // {
+        dir = "bin";
+        isExecutable = true;
+        meta.mainProgram = attrs.name;
+      }
+    );
 in
 {
-  darwin-option = writeProgram "darwin-option"
-    {
+  darwin-option = writeProgram {
+    name = "darwin-option";
+    src = ./darwin-option.sh;
+
+    replacements = {
       inherit path nixPath;
       inherit (stdenv) shell;
-    }
-    ./darwin-option.sh;
+    };
+  };
 
-  darwin-rebuild = writeProgram "darwin-rebuild"
-    {
+  darwin-rebuild = writeProgram {
+    name = "darwin-rebuild";
+    src = ./darwin-rebuild.sh;
+
+    replacements = {
       inherit path nixPath profile;
       inherit (stdenv) shell;
-      postInstall = ''
-        mkdir -p $out/share/zsh/site-functions
-        cp ${./darwin-rebuild.zsh-completions} $out/share/zsh/site-functions/_darwin-rebuild
-      '';
-    }
-    ./darwin-rebuild.sh;
+    };
+
+    postInstall = ''
+      mkdir -p $out/share/zsh/site-functions
path: pkgs/nix-tools/default.nix
user: login: Enzime
body: Can you remove all the reformatting?
created_at: 2025-05-01T15:20:32Z
author_association: MEMBER
diff_hunk: @@ -1,70 +1,89 @@
-{ lib
-, coreutils
-, jq
-, git
-, substituteAll
-, stdenv
-, profile ? "/nix/var/nix/profiles/system"
-, # This should be kept in sync with the default
+{
+  lib,
+  coreutils,
+  jq,
+  git,
+  replaceVarsWith,
+  stdenv,
+  profile ? "/nix/var/nix/profiles/system",
+  # This should be kept in sync with the default
   # `environment.systemPath`. We err on side of including conditional
   # things like the profile directories, since they’re more likely to
   # help than hurt, and this default is mostly used for fresh
   # installations anyway.
   systemPath ? lib.concatStringsSep ":" [
-  "$HOME/.nix-profile/bin"
-  "/etc/profiles/per-user/$USER/bin"
-  "/run/current-system/sw/bin"
-  "/nix/var/nix/profiles/default/bin"
-  "/usr/local/bin"
-  "/usr/bin"
-  "/bin"
-  "/usr/sbin"
-  "/sbin"
-]
-, nixPackage ? null
-, # This should be kept in sync with the default `nix.nixPath`.
+    "$HOME/.nix-profile/bin"
+    "/etc/profiles/per-user/$USER/bin"
+    "/run/current-system/sw/bin"
+    "/nix/var/nix/profiles/default/bin"
+    "/usr/local/bin"
+    "/usr/bin"
+    "/bin"
+    "/usr/sbin"
+    "/sbin"
+  ],
+  nixPackage ? null,
+  # This should be kept in sync with the default `nix.nixPath`.
   nixPath ? lib.concatStringsSep ":" [
-  "darwin-config=/etc/nix-darwin/configuration.nix"
-  "/nix/var/nix/profiles/per-user/root/channels"
-]
+    "darwin-config=/etc/nix-darwin/configuration.nix"
+    "/nix/var/nix/profiles/per-user/root/channels"
+  ],
 }:
 
 let
-  extraPath = lib.makeBinPath [ coreutils jq git nixPackage ];
-
-  writeProgram = name: env: src:
-    substituteAll ({
-      inherit name src;
-      dir = "bin";
-      isExecutable = true;
-      meta.mainProgram = name;
-    } // env);
+  extraPath = lib.makeBinPath [
+    coreutils
+    jq
+    git
+    nixPackage
+  ];
 
   path = "${extraPath}:${systemPath}";
+
+  writeProgram =
+    attrs:
+    replaceVarsWith (
+      attrs
+      // {
+        dir = "bin";
+        isExecutable = true;
+        meta.mainProgram = attrs.name;
+      }
+    );
 in
 {
-  darwin-option = writeProgram "darwin-option"
-    {
+  darwin-option = writeProgram {
+    name = "darwin-option";
+    src = ./darwin-option.sh;
+
+    replacements = {
       inherit path nixPath;
       inherit (stdenv) shell;
-    }
-    ./darwin-option.sh;
+    };
+  };
 
-  darwin-rebuild = writeProgram "darwin-rebuild"
-    {
+  darwin-rebuild = writeProgram {
+    name = "darwin-rebuild";
+    src = ./darwin-rebuild.sh;
+
+    replacements = {
       inherit path nixPath profile;
       inherit (stdenv) shell;
-      postInstall = ''
-        mkdir -p $out/share/zsh/site-functions
-        cp ${./darwin-rebuild.zsh-completions} $out/share/zsh/site-functions/_darwin-rebuild
-      '';
-    }
-    ./darwin-rebuild.sh;
+    };
+
+    postInstall = ''
+      mkdir -p $out/share/zsh/site-functions
path: pkgs/nix-tools/default.nix
user: login: WeetHet
body: Done
created_at: 2025-05-01T15:24:03Z
author_association: CONTRIBUTOR

PR comments:
user: login: WeetHet
created_at: 2025-05-01T15:18:45Z
body: @Enzime could you review please
author_association: CONTRIBUTOR
user: login: Enzime
created_at: 2025-05-03T06:48:41Z
body: Tested using:

```
$ export OUTPUT=darwin-option; shasum -a 256 $(nix build github:nix-darwin/nix-darwin#$OUTPUT --override-input nixpkgs nixpkgs/$(nix flake metadata github:nix-darwin/nix-darwin/pull/1432/head --json | jq -r '.locks.nodes.nixpkgs.locked.rev') --print-out-paths --no-link)/bin/$OUTPUT $(nix build github:nix-darwin/nix-darwin/pull/1432/head#$OUTPUT --print-out-paths --no-link)/bin/$OUTPUT
warning: not writing modified lock file of flake 'github:nix-darwin/nix-darwin':
• Updated input 'nixpkgs':
    'github:NixOS/nixpkgs/8c9fd3e564728e90829ee7dbac6edc972971cd0f?narHash=sha256-CHd7yhaDigUuJyDeX0SADbTM9FXfiWaeNyY34FL1wQU%3D' (2025-01-07)
  → 'github:NixOS/nixpkgs/3afd19146cac33ed242fc0fc87481c67c758a59e?narHash=sha256-OxYwCGJf9VJ2KnUO%2Bw/hVJVTjOgscdDg/lPv8Eus07Y%3D' (2025-05-01)
b451e02c33f8c855cb3d9b49ac9c427744b86eeaa7f5f70c0148e39c9ed12e98  /nix/store/p8vm7id1ik017qg5fl3x3vrahcadzmv8-darwin-option/bin/darwin-option
b451e02c33f8c855cb3d9b49ac9c427744b86eeaa7f5f70c0148e39c9ed12e98  /nix/store/7kjdx8a7dfvj1d10vn6d0gn5hdbxg2ij-darwin-option/bin/darwin-option
```
author_association: MEMBER