12 cfg = config.boot.initrd.network.openvpn;
20 boot.initrd.network.openvpn.enable = mkOption {
24 Starts an OpenVPN client during initrd boot. It can be used to e.g.
25 remotely accessing the SSH service controlled by
26 {option}`boot.initrd.network.ssh` or other network services
27 included. Service is killed when stage-1 boot is finished.
31 boot.initrd.network.openvpn.configuration = mkOption {
32 type = types.path; # Same type as boot.initrd.secrets
34 The configuration file for OpenVPN.
37 Unless your bootloader supports initrd secrets, this configuration
38 is stored insecurely in the global Nix store.
41 example = literalExpression "./configuration.ovpn";
46 config = mkIf (config.boot.initrd.network.enable && cfg.enable) {
49 assertion = cfg.configuration != null;
50 message = "You should specify a configuration for initrd OpenVPN";
54 # Add kernel modules needed for OpenVPN
55 boot.initrd.kernelModules = [
60 # Add openvpn and ip binaries to the initrd
61 # The shared libraries are required for DNS resolution
62 boot.initrd.extraUtilsCommands = mkIf (!config.boot.initrd.systemd.enable) ''
63 copy_bin_and_libs ${pkgs.openvpn}/bin/openvpn
64 copy_bin_and_libs ${pkgs.iproute2}/bin/ip
66 cp -pv ${pkgs.glibc}/lib/libresolv.so.2 $out/lib
67 cp -pv ${pkgs.glibc}/lib/libnss_dns.so.2 $out/lib
70 boot.initrd.systemd.storePaths = [
71 "${pkgs.openvpn}/bin/openvpn"
72 "${pkgs.iproute2}/bin/ip"
73 "${pkgs.glibc}/lib/libresolv.so.2"
74 "${pkgs.glibc}/lib/libnss_dns.so.2"
77 boot.initrd.secrets = {
78 "/etc/initrd.ovpn" = cfg.configuration;
81 # openvpn --version would exit with 1 instead of 0
82 boot.initrd.extraUtilsCommandsTest = mkIf (!config.boot.initrd.systemd.enable) ''
83 $out/bin/openvpn --show-gateway
86 boot.initrd.network.postCommands = mkIf (!config.boot.initrd.systemd.enable) ''
87 openvpn /etc/initrd.ovpn &
90 boot.initrd.systemd.services.openvpn = {
91 wantedBy = [ "initrd.target" ];
92 path = [ pkgs.iproute2 ];
95 "initrd-nixos-copy-secrets.service"
97 serviceConfig.ExecStart = "${pkgs.openvpn}/bin/openvpn /etc/initrd.ovpn";
98 serviceConfig.Type = "notify";