2024-01-06 01:19:35 +01:00
|
|
|
# SPDX-FileCopyrightText: 2021-2022 Simon Bruder <simon@sbruder.de>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2021-05-31 23:02:11 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
relays = builtins.fromJSON (builtins.readFile ./relays.json);
|
|
|
|
|
|
|
|
cfg = config.sbruder.mullvad;
|
|
|
|
|
|
|
|
relayConfigs = lib.mapAttrs'
|
2022-09-30 12:02:46 +02:00
|
|
|
(name: configuration: lib.nameValuePair "mlv-${name}.conf" (with configuration; ''
|
2021-05-31 23:02:11 +02:00
|
|
|
[Interface]
|
|
|
|
DNS = ${cfg.dnsServer}
|
|
|
|
|
|
|
|
[Peer]
|
|
|
|
Endpoint = ${if cfg.ipVersion == 4 then endpoint4 else endpoint6}:${toString cfg.port}
|
|
|
|
PublicKey = ${pubkey}
|
|
|
|
AllowedIPs = 0.0.0.0/0,::0/0
|
|
|
|
''))
|
|
|
|
relays;
|
|
|
|
|
|
|
|
# Creating 100+ files in a separate derivation each has too much overhead
|
|
|
|
relayConfigFiles = pkgs.runCommandNoCC "etc-wireguard-mullvad" { } (''
|
|
|
|
mkdir $out
|
|
|
|
'' + (lib.concatStringsSep
|
|
|
|
"\n"
|
|
|
|
(lib.mapAttrsToList
|
|
|
|
(name: content: ''
|
|
|
|
cat > $out/${lib.escapeShellArg name} << EOF
|
|
|
|
${content}
|
|
|
|
EOF
|
|
|
|
'')
|
|
|
|
relayConfigs)));
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.sbruder.mullvad = {
|
|
|
|
enable = lib.mkEnableOption "wg-quick compatible configuration files in /etc/wireguard for Mullvad VPN";
|
|
|
|
dnsServer = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "193.138.218.74";
|
|
|
|
};
|
|
|
|
ipVersion = lib.mkOption {
|
|
|
|
type = lib.types.enum [ 4 6 ];
|
|
|
|
default = 4;
|
|
|
|
};
|
|
|
|
port = lib.mkOption {
|
|
|
|
type = lib.types.port;
|
|
|
|
default = 51820;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
2021-06-01 10:29:58 +02:00
|
|
|
environment = {
|
|
|
|
etc = builtins.listToAttrs
|
|
|
|
(map
|
|
|
|
(name: lib.nameValuePair "wireguard/${name}" { source = "${relayConfigFiles}/${name}"; })
|
|
|
|
(lib.attrNames relayConfigs));
|
|
|
|
|
2022-09-30 12:22:28 +02:00
|
|
|
systemPackages = lib.singleton (pkgs.runCommandNoCC "mullvad-on-demand" { } ''
|
|
|
|
install -D ${./mullvad.sh} $out/bin/mullvad
|
|
|
|
install -D ${./mullvad-fzf.sh} $out/bin/mullvad-fzf
|
|
|
|
'');
|
2021-06-01 10:29:58 +02:00
|
|
|
};
|
2021-05-31 23:02:11 +02:00
|
|
|
};
|
|
|
|
}
|