aboutsummaryrefslogtreecommitdiffstats
path: root/modules/networking/usque/default.nix
blob: 9a639dd098e5204e7dc3e97dd569b9988b66f9a2 (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
# Open-source reimplementation of the Cloudflare WARP client's MASQUE protocol
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.networking.usque;
  icons = config.desktop.icons.apps;

  usqueWarpScript = pkgs.writeShellApplication {
    name = "usque-warp";
    runtimeInputs = [
      cfg.package
      config.terminal.bash.package
    ];
    text = builtins.readFile ./usque-warp.sh;
  };

  usqueWarpCompletion =
    pkgs.runCommand "usque-warp-completion"
      {
        nativeBuildInputs = [ pkgs.installShellFiles ];
      }
      ''
        mkdir -p $out/share/zsh/site-functions
        installShellCompletion --zsh --name _usque-warp ${pkgs.writeText "usque-warp.zsh" (builtins.readFile ./usque-warp.zsh)}
      '';

  usqueWarpScriptPkg = pkgs.symlinkJoin {
    name = "usque-warp";
    paths = [
      usqueWarpScript
      usqueWarpCompletion
    ];
    meta = usqueWarpScript.meta or { };
  };
in
{
  imports = [ ./assertions.nix ];

  options.networking.usque = {
    enable = lib.mkEnableOption "Enables usque.";
    package = lib.mkOption {
      type = lib.types.package;
      default = pkgs.usque;
      description = "The usque package to use.";
    };

    warp = lib.mkOption {
      type = lib.types.submodule {
        options = {
          enable = lib.mkEnableOption "Enables the usque-warp script.";
          package = lib.mkOption {
            type = lib.types.package;
            readOnly = true;
            default = usqueWarpScriptPkg;
            description = "The usque-warp script package.";
          };
        };
      };
      default = { };
      description = "WARP script configuration.";
    };
  };

  config = lib.mkMerge [
    (lib.mkIf cfg.enable {
      home.packages = [ cfg.package ];
    })
    (lib.mkIf cfg.warp.enable {
      home.packages = [ cfg.warp.package ];
    })
    (lib.mkIf (cfg.warp.enable && config.desktop.enable) {
      desktop.rofi.action.actions = [
        {
          name = "WARP Connect";
          command = "${lib.getExe cfg.warp.package} connect";
          icon = icons.usqueVpn;
        }
        {
          name = "WARP Disconnect";
          command = "${lib.getExe cfg.warp.package} disconnect";
          icon = icons.usqueVpnDisconnected;
        }
      ];
    })
  ];
}