2020-07-18 16:06:22 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.boot.initrd.network.openvpn;
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2020-07-18 16:06:22 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
boot.initrd.network.openvpn.enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-08-20 17:08:02 +00:00
|
|
|
Starts an OpenVPN client during initrd boot. It can be used to e.g.
|
|
|
|
remotely accessing the SSH service controlled by
|
2022-08-12 12:06:08 +00:00
|
|
|
{option}`boot.initrd.network.ssh` or other network services
|
2020-07-18 16:06:22 +00:00
|
|
|
included. Service is killed when stage-1 boot is finished.
|
|
|
|
'';
|
|
|
|
};
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2020-07-18 16:06:22 +00:00
|
|
|
boot.initrd.network.openvpn.configuration = mkOption {
|
|
|
|
type = types.path; # Same type as boot.initrd.secrets
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-08-20 17:08:02 +00:00
|
|
|
The configuration file for OpenVPN.
|
2020-07-18 16:06:22 +00:00
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
::: {.warning}
|
|
|
|
Unless your bootloader supports initrd secrets, this configuration
|
|
|
|
is stored insecurely in the global Nix store.
|
|
|
|
:::
|
2020-07-18 16:06:22 +00:00
|
|
|
'';
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression "./configuration.ovpn";
|
2020-07-18 16:06:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf (config.boot.initrd.network.enable && cfg.enable) {
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = cfg.configuration != null;
|
|
|
|
message = "You should specify a configuration for initrd OpenVPN";
|
|
|
|
}
|
|
|
|
];
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2020-07-18 16:06:22 +00:00
|
|
|
# Add kernel modules needed for OpenVPN
|
|
|
|
boot.initrd.kernelModules = [ "tun" "tap" ];
|
|
|
|
|
|
|
|
# Add openvpn and ip binaries to the initrd
|
|
|
|
# The shared libraries are required for DNS resolution
|
2023-04-29 16:46:19 +00:00
|
|
|
boot.initrd.extraUtilsCommands = mkIf (!config.boot.initrd.systemd.enable) ''
|
2020-07-18 16:06:22 +00:00
|
|
|
copy_bin_and_libs ${pkgs.openvpn}/bin/openvpn
|
2021-04-08 16:26:57 +00:00
|
|
|
copy_bin_and_libs ${pkgs.iproute2}/bin/ip
|
2020-07-18 16:06:22 +00:00
|
|
|
|
|
|
|
cp -pv ${pkgs.glibc}/lib/libresolv.so.2 $out/lib
|
|
|
|
cp -pv ${pkgs.glibc}/lib/libnss_dns.so.2 $out/lib
|
|
|
|
'';
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2023-04-29 16:46:19 +00:00
|
|
|
boot.initrd.systemd.storePaths = [
|
|
|
|
"${pkgs.openvpn}/bin/openvpn"
|
|
|
|
"${pkgs.iproute2}/bin/ip"
|
|
|
|
"${pkgs.glibc}/lib/libresolv.so.2"
|
|
|
|
"${pkgs.glibc}/lib/libnss_dns.so.2"
|
|
|
|
];
|
|
|
|
|
2020-07-18 16:06:22 +00:00
|
|
|
boot.initrd.secrets = {
|
|
|
|
"/etc/initrd.ovpn" = cfg.configuration;
|
|
|
|
};
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2020-07-18 16:06:22 +00:00
|
|
|
# openvpn --version would exit with 1 instead of 0
|
2023-04-29 16:46:19 +00:00
|
|
|
boot.initrd.extraUtilsCommandsTest = mkIf (!config.boot.initrd.systemd.enable) ''
|
2020-07-18 16:06:22 +00:00
|
|
|
$out/bin/openvpn --show-gateway
|
|
|
|
'';
|
|
|
|
|
2023-04-29 16:46:19 +00:00
|
|
|
boot.initrd.network.postCommands = mkIf (!config.boot.initrd.systemd.enable) ''
|
2022-12-28 21:21:41 +00:00
|
|
|
openvpn /etc/initrd.ovpn &
|
2020-07-18 16:06:22 +00:00
|
|
|
'';
|
2023-04-29 16:46:19 +00:00
|
|
|
|
|
|
|
boot.initrd.systemd.services.openvpn = {
|
|
|
|
wantedBy = [ "initrd.target" ];
|
|
|
|
path = [ pkgs.iproute2 ];
|
|
|
|
after = [ "network.target" "initrd-nixos-copy-secrets.service" ];
|
|
|
|
serviceConfig.ExecStart = "${pkgs.openvpn}/bin/openvpn /etc/initrd.ovpn";
|
|
|
|
serviceConfig.Type = "notify";
|
|
|
|
};
|
2020-07-18 16:06:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|