about summary refs log tree commit diff
path: root/nix
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2020-06-28T02·52+0200
committerProfpatsch <mail@profpatsch.de>2020-06-30T00·19+0000
commit2f21e0c8c0b47dcf1b29c38a32838f48bd873c0b (patch)
tree885b671e4c9f5ee887700130dc4bfba9926dab49 /nix
parent4402c363b6137112716e82b93e946634aefbb600 (diff)
refactor(nix): use our own writeScript(Bin) r/1133
We have this nice `runExecline` now, so we don’t need to use
`runCommand` (which spawns bash) just to write a simple script.

Change-Id: I2941ed8c1448fa1d7cc02dc18b24a8a945b2c38b
Reviewed-on: https://cl.tvl.fyi/c/depot/+/704
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
Reviewed-by: BuildkiteCI
Diffstat (limited to 'nix')
-rw-r--r--nix/binify/default.nix2
-rw-r--r--nix/getBins/default.nix2
-rw-r--r--nix/runExecline/default.nix2
-rw-r--r--nix/runExecline/tests.nix19
-rw-r--r--nix/writeExecline/default.nix3
-rw-r--r--nix/writeScript/default.nix29
-rw-r--r--nix/writeScriptBin/default.nix12
7 files changed, 46 insertions, 23 deletions
diff --git a/nix/binify/default.nix b/nix/binify/default.nix
index 17fd980ad994..d40930fd3334 100644
--- a/nix/binify/default.nix
+++ b/nix/binify/default.nix
@@ -10,7 +10,7 @@
 # with `binify { exe = …; name = "hello" }`.
 { exe, name }:
 
-pkgs.runCommandLocal "binify-${name}" {} ''
+pkgs.runCommandLocal "${name}-bin" {} ''
   mkdir -p $out/bin
   ln -sT ${lib.escapeShellArg exe} $out/bin/${lib.escapeShellArg name}
 ''
diff --git a/nix/getBins/default.nix b/nix/getBins/default.nix
index b0c1135fc87a..5ba7584ed844 100644
--- a/nix/getBins/default.nix
+++ b/nix/getBins/default.nix
@@ -38,7 +38,7 @@ let
 
   tests = import ./tests.nix {
     inherit getBins;
-    inherit (pkgs) writeScriptBin;
+    inherit (depot.nix) writeScriptBin;
     inherit (depot.nix.runTestsuite) assertEq it runTestsuite;
   };
 
diff --git a/nix/runExecline/default.nix b/nix/runExecline/default.nix
index 0b87b1366d27..22d968a1c643 100644
--- a/nix/runExecline/default.nix
+++ b/nix/runExecline/default.nix
@@ -8,7 +8,7 @@ let
 
   tests = import ./tests.nix {
     inherit runExecline;
-    inherit (depot.nix) getBins;
+    inherit (depot.nix) getBins writeScript;
     inherit (pkgs) stdenv coreutils;
     inherit pkgs;
   };
diff --git a/nix/runExecline/tests.nix b/nix/runExecline/tests.nix
index cd7f4220b0e3..a8f91f3cf3ae 100644
--- a/nix/runExecline/tests.nix
+++ b/nix/runExecline/tests.nix
@@ -1,4 +1,4 @@
-{ stdenv, pkgs, runExecline, getBins
+{ stdenv, pkgs, runExecline, getBins, writeScript
 # https://www.mail-archive.com/skaware@list.skarnet.org/msg01256.html
 , coreutils }:
 
@@ -19,23 +19,6 @@ let
            "s6-test"
          ];
 
-  # lol
-  writeScript = name: script: runExecline name {
-    derivationArgs = {
-      inherit script;
-      passAsFile = [ "script" ];
-      preferLocalBuild = true;
-      allowSubstitutes = false;
-    };
-  } [
-      "importas" "-ui" "s" "scriptPath"
-      "importas" "-ui" "out" "out"
-      "foreground" [
-        bins.mv "$s" "$out"
-      ]
-      bins.s6-chmod "0755" "$out"
-  ];
-
   # execline block of depth 1
   block = args: builtins.map (arg: " ${arg}") args ++ [ "" ];
 
diff --git a/nix/writeExecline/default.nix b/nix/writeExecline/default.nix
index 49bc416094a3..8626aa46080f 100644
--- a/nix/writeExecline/default.nix
+++ b/nix/writeExecline/default.nix
@@ -32,8 +32,7 @@ let
     else abort ''"${toString argMode}" is not a valid argMode, use one of "var", "var-full", "env", "none", "env-no-push".'';
 
 in
-  # TODO(Profpatsch): rewrite `writeScript` with `runExecline`
-  pkgs.writeScript name ''
+  depot.nix.writeScript name ''
     #!${pkgs.execline}/bin/execlineb -W${env}
     ${depot.nix.escapeExecline argList}
   ''
diff --git a/nix/writeScript/default.nix b/nix/writeScript/default.nix
new file mode 100644
index 000000000000..e8e6e0fa10ac
--- /dev/null
+++ b/nix/writeScript/default.nix
@@ -0,0 +1,29 @@
+{ pkgs, depot, ... }:
+
+# Write the given string to $out
+# and make it executable.
+
+let
+  bins = depot.nix.getBins pkgs.s6-portable-utils [
+           "s6-cat"
+           "s6-chmod"
+         ];
+
+in
+name:
+# string of the executable script that is put in $out
+script:
+
+depot.nix.runExecline name {
+  stdin = script;
+  derivationArgs = {
+    preferLocalBuild = true;
+    allowSubstitutes = false;
+  };
+} [
+  "importas" "out" "out"
+  # this pipes stdout of s6-cat to $out
+  # and s6-cat redirects from stdin to stdout
+  "if" [ "redirfd" "-w" "1" "$out" bins.s6-cat ]
+  bins.s6-chmod "0755" "$out"
+]
diff --git a/nix/writeScriptBin/default.nix b/nix/writeScriptBin/default.nix
new file mode 100644
index 000000000000..ed26cf197e1e
--- /dev/null
+++ b/nix/writeScriptBin/default.nix
@@ -0,0 +1,12 @@
+{ depot, ... }:
+
+# Like writeScript,
+# but put the script into `$out/bin/${name}`.
+
+name:
+script:
+
+depot.nix.binify {
+  exe = (depot.nix.writeScript name script);
+  inherit name;
+}