about summary refs log tree commit diff
path: root/users/Profpatsch/nixpkgs-rewriter/default.nix
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2021-01-10T19·56+0100
committerProfpatsch <mail@profpatsch.de>2021-01-10T20·03+0000
commit2f807d7f141068d2d60676a89213eaa5353ca6e0 (patch)
tree4f2b13aab630c4febb5d9ccd3d701565009b6c39 /users/Profpatsch/nixpkgs-rewriter/default.nix
parentc1cb4c260c97ad83a68f323dfeb4534f972c375a (diff)
feat(users/Profpatsch): add a rewriter for lib.stdenv changes r/2070
This is in order to advance the rewriting from stdenv.lib to lib.
https://github.com/NixOS/nixpkgs/issues/108938

The hard part about changing the argument is that a package might not
include lib in its arguments, which is why I use hnix to check whether
lib is included and add it to the import list if it doesn’t already
exist there.

So far, only the really common pattern of

    meta = with stdenv.lib;

is rewritten.

Change-Id: I370f0a321b0e5a5bd21ec21fc7cefdd65ec845ed
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2345
Tested-by: BuildkiteCI
Reviewed-by: Profpatsch <mail@profpatsch.de>
Diffstat (limited to 'users/Profpatsch/nixpkgs-rewriter/default.nix')
-rw-r--r--users/Profpatsch/nixpkgs-rewriter/default.nix84
1 files changed, 84 insertions, 0 deletions
diff --git a/users/Profpatsch/nixpkgs-rewriter/default.nix b/users/Profpatsch/nixpkgs-rewriter/default.nix
new file mode 100644
index 000000000000..ab86ff32090c
--- /dev/null
+++ b/users/Profpatsch/nixpkgs-rewriter/default.nix
@@ -0,0 +1,84 @@
+{ depot, pkgs, ... }:
+let
+  inherit (depot.nix)
+    writeExecline
+    ;
+  inherit (depot.users.Profpatsch.lib)
+    debugExec
+    eprintf
+    ;
+
+  export-json-object = pkgs.writers.writePython3 "export-json-object" {} ''
+    import json
+    import sys
+    import os
+
+    d = json.load(sys.stdin)
+
+    if d == {}:
+        sys.exit(0)
+
+    for k, v in d.items():
+        os.environ[k] = str(v)
+
+    os.execvp(sys.argv[1], sys.argv[1:])
+  '';
+
+  meta-stdenv-lib = pkgs.writers.writeHaskell "meta-stdenv-lib" {
+    libraries = [
+      pkgs.haskellPackages.hnix
+      pkgs.haskellPackages.aeson
+    ];
+  } ./MetaStdenvLib.hs;
+
+  replace-between-lines = writeExecline "replace-between-lines" { readNArgs = 1; } [
+    "importas" "-ui" "file" "fileName"
+    "importas" "-ui" "from" "fromLine"
+    "importas" "-ui" "to" "toLine"
+    "if" [ eprintf "\${from}-\${to}" ]
+    (debugExec "adding lib")
+    "sed"
+      "-e" "\${from},\${to} \${1}"
+      "-i" "$file"
+  ];
+
+  add-lib-if-necessary = writeExecline "add-lib-if-necessary" { readNArgs = 1; } [
+    "pipeline" [ meta-stdenv-lib "$1" ]
+     export-json-object
+     # first replace any stdenv.lib mentions in the arg header
+     # if this is not done, the replace below kills these.
+     # Since we want it anyway ultimately, let’s do it here.
+     "if" [ replace-between-lines "s/stdenv\.lib/lib/" ]
+     # then add the lib argument
+     # (has to be before stdenv, otherwise default arguments might be in the way)
+     replace-between-lines "s/stdenv/lib, stdenv/"
+  ];
+
+  metaString = ''meta = with stdenv.lib; {'';
+
+  replace-stdenv-lib = pkgs.writers.writeBash "replace-stdenv-lib" ''
+    set -euo pipefail
+    sourceDir="$1"
+    for file in $(
+      ${pkgs.ripgrep}/bin/rg \
+        --files-with-matches \
+        --fixed-strings \
+        -e '${metaString}' \
+        "$sourceDir"
+    )
+    do
+      echo "replacing stdenv.lib meta in $file" >&2
+      sed -e '/${metaString}/ s/stdenv.lib/lib/' \
+          -i "$file"
+      ${add-lib-if-necessary} "$file"
+    done
+  '';
+
+in {
+  # requires hnix, which we don’t want in tvl for now
+  # uncomment manually if you want to use it.
+  # inherit
+  #   meta-stdenv-lib
+  #   replace-stdenv-lib
+  #   ;
+}