1 { config, lib, pkgs, ... }:
7 cfg = config.boot.initrd.network.openvpn;
15 boot.initrd.network.openvpn.enable = mkOption {
19 Starts an OpenVPN client during initrd boot. It can be used to e.g.
20 remotely accessing the SSH service controlled by
21 {option}`boot.initrd.network.ssh` or other network services
22 included. Service is killed when stage-1 boot is finished.
26 boot.initrd.network.openvpn.configuration = mkOption {
27 type = types.path; # Same type as boot.initrd.secrets
29 The configuration file for OpenVPN.
32 Unless your bootloader supports initrd secrets, this configuration
33 is stored insecurely in the global Nix store.
36 example = literalExpression "./configuration.ovpn";
41 config = mkIf (config.boot.initrd.network.enable && cfg.enable) {
44 assertion = cfg.configuration != null;
45 message = "You should specify a configuration for initrd OpenVPN";
49 # Add kernel modules needed for OpenVPN
50 boot.initrd.kernelModules = [ "tun" "tap" ];
52 # Add openvpn and ip binaries to the initrd
53 # The shared libraries are required for DNS resolution
54 boot.initrd.extraUtilsCommands = mkIf (!config.boot.initrd.systemd.enable) ''
55 copy_bin_and_libs ${pkgs.openvpn}/bin/openvpn
56 copy_bin_and_libs ${pkgs.iproute2}/bin/ip
58 cp -pv ${pkgs.glibc}/lib/libresolv.so.2 $out/lib
59 cp -pv ${pkgs.glibc}/lib/libnss_dns.so.2 $out/lib
62 boot.initrd.systemd.storePaths = [
63 "${pkgs.openvpn}/bin/openvpn"
64 "${pkgs.iproute2}/bin/ip"
65 "${pkgs.glibc}/lib/libresolv.so.2"
66 "${pkgs.glibc}/lib/libnss_dns.so.2"
69 boot.initrd.secrets = {
70 "/etc/initrd.ovpn" = cfg.configuration;
73 # openvpn --version would exit with 1 instead of 0
74 boot.initrd.extraUtilsCommandsTest = mkIf (!config.boot.initrd.systemd.enable) ''
75 $out/bin/openvpn --show-gateway
78 boot.initrd.network.postCommands = mkIf (!config.boot.initrd.systemd.enable) ''
79 openvpn /etc/initrd.ovpn &
82 boot.initrd.systemd.services.openvpn = {
83 wantedBy = [ "initrd.target" ];
84 path = [ pkgs.iproute2 ];
85 after = [ "network.target" "initrd-nixos-copy-secrets.service" ];
86 serviceConfig.ExecStart = "${pkgs.openvpn}/bin/openvpn /etc/initrd.ovpn";
87 serviceConfig.Type = "notify";