about summary refs log tree commit diff
path: root/users/Profpatsch/nixpkgs-rewriter/default.nix
blob: 9dac0184416540122a6f2bc6ea08d52b7747aa81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{ depot, pkgs, ... }:
let
  inherit (depot.nix)
    writeExecline
    ;
  inherit (depot.users.Profpatsch.lib)
    debugExec
    ;

  bins = depot.nix.getBins pkgs.coreutils [ "head" "shuf" ]
      // depot.nix.getBins pkgs.jq [ "jq" ]
      // depot.nix.getBins pkgs.findutils [ "xargs" ]
      // depot.nix.getBins pkgs.gnused [ "sed" ]
      ;

  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" [ depot.tools.eprintf "%s-%s\n" "$from" "$to" ]
    (debugExec "adding lib")
    bins.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
      ${bins.sed} -e '/${metaString}/ s/stdenv.lib/lib/' \
          -i "$file"
      ${add-lib-if-necessary} "$file"
    done
  '';

  instantiate-nixpkgs-randomly = writeExecline "instantiate-nixpkgs-randomly" { readNArgs = 1; } [
    "export" "NIXPKGS_ALLOW_BROKEN" "1"
    "export" "NIXPKGS_ALLOW_UNFREE" "1"
    "export" "NIXPKGS_ALLOW_INSECURE" "1"
    "export" "NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM" "1"
    "pipeline" [
      "nix"
        "eval"
        "--raw"
        ''(
          let pkgs = import ''${1} {};
          in builtins.toJSON (builtins.attrNames pkgs)
        )''
    ]
    "pipeline" [ bins.jq "-r" ".[]" ]
    "pipeline" [ bins.shuf ]
    "pipeline" [ bins.head "-n" "1000" ]
    bins.xargs "-I" "{}" "-n1"
    "if" [ depot.tools.eprintf "instantiating %s\n" "{}" ]
    "nix-instantiate" "$1" "-A" "{}"
  ];

in depot.nix.utils.drvTargets {
  inherit
   instantiate-nixpkgs-randomly
  # requires hnix, which we don’t want in tvl for now
  # uncomment manually if you want to use it.
  #   meta-stdenv-lib
  #   replace-stdenv-lib
    ;
}