aboutsummaryrefslogtreecommitdiffstats
path: root/modules/scripts/lib.nix
blob: cab897d931db30f3ebbc6fc66577673f45232a9b (plain)
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
{ lib, pkgs }:
let
  shell = pkgs.runtimeShell;

  mkScript =
    name: path: description: env: deps:
    let
      descLines = lib.filter (s: s != "") (lib.splitString "\n" description);
      descComment =
        if descLines == [ ] then "" else lib.concatMapStringsSep "\n" (line: "# ${line}") descLines + "\n";
      envVars = lib.concatStringsSep "\n" (lib.mapAttrsToList (n: v: "${n}=\"${toString v}\"") env);
      wrappedScript = pkgs.writeTextFile {
        name = name;
        executable = true;
        destination = "/bin/${name}";
        text = ''
          #!${shell}
          ${descComment}${envVars}
          ${builtins.readFile path}
        '';
      };
    in
    pkgs.runCommand name
      {
        nativeBuildInputs = [ pkgs.makeWrapper ];
        meta.mainProgram = name;
      }
      ''
        mkdir -p $out/bin
        cp ${wrappedScript}/bin/${name} $out/bin/${name}
        chmod +x $out/bin/${name}

        wrapProgram $out/bin/${name} \
          --prefix PATH : ${lib.makeBinPath deps}
      '';

  mkScriptModule =
    {
      name,
      path,
      env ? { },
      deps ? [ ],
      desktop ? null,
      extraLinks ? [ ],
      config,
      description ? "",
    }:
    let
      scriptDrv = mkScript name path description env deps;
    in
    {
      options.scripts.${name} = {
        enable = lib.mkOption {
          type = lib.types.bool;
          default = true;
          description = "Whether to enable the ${name} script.";
        };
        package = lib.mkOption {
          type = lib.types.nullOr lib.types.package;
          readOnly = true;
          description = "The derivation for the ${name} script.";
        };
        desktop = {
          enable = lib.mkOption {
            type = lib.types.bool;
            default = false;
            description = "Whether to create a .desktop entry for this script.";
          };
          displayName = lib.mkOption {
            type = lib.types.str;
            default = name;
            description = "Display name for the .desktop entry.";
          };
        };
      };

      moduleConfig = {
        scripts.${name} = lib.mkIf config.scripts.${name}.enable (
          {
            package = scriptDrv;
          }
          // lib.optionalAttrs (desktop != null) { inherit desktop; }
        );

        home.file = lib.mkIf config.scripts.${name}.enable (
          builtins.listToAttrs (
            map (linkPath: {
              name = lib.removePrefix "~/" linkPath;
              value = {
                source = "${scriptDrv}/bin/${name}";
              };
            }) extraLinks
          )
        );

        assertions = [
          {
            assertion =
              !config.scripts.${name}.enable
              || !config.scripts.${name}.desktop.enable
              || config.terminal.kitty.enable;
            message = "scripts.${name}.desktop.enable requires terminal.kitty.enable because desktop entries launch scripts in kitty.";
          }
        ];
      };
    };
in
{
  inherit mkScript mkScriptModule;
}