diff --git a/nixos/modules/services/networking/ssh/sshd.nix b/nixos/modules/services/networking/ssh/sshd.nix index 1e4e34a4f1675..0d127a6544c05 100644 --- a/nixos/modules/services/networking/ssh/sshd.nix +++ b/nixos/modules/services/networking/ssh/sshd.nix @@ -1,7 +1,4 @@ { config, lib, pkgs, ... }: - -with lib; - let # The splicing information needed for nativeBuildInputs isn't available @@ -17,8 +14,8 @@ let let # reports boolean as yes / no mkValueString = with lib; v: - if isInt v then toString v - else if isString v then v + if lib.isInt v then toString v + else if lib.isString v then v else if true == v then "yes" else if false == v then "no" else throw "unsupported type ${builtins.typeOf v}: ${(lib.generators.toPretty {}) v}"; @@ -37,10 +34,10 @@ let in { inherit (base) type; generate = name: value: - let transformedValue = mapAttrs (key: val: - if isList val then - if elem key commaSeparated then concatStringsSep "," val - else if elem key spaceSeparated then concatStringsSep " " val + let transformedValue = lib.mapAttrs (key: val: + if lib.isList val then + if lib.elem key commaSeparated then lib.concatStringsSep "," val + else if lib.elem key spaceSeparated then lib.concatStringsSep " " val else throw "list value for unknown key ${key}: ${(lib.generators.toPretty {}) val}" else val @@ -49,7 +46,7 @@ let base.generate name transformedValue; }; - configFile = settingsFormat.generate "sshd.conf-settings" (filterAttrs (n: v: v != null) cfg.settings); + configFile = settingsFormat.generate "sshd.conf-settings" (lib.filterAttrs (n: v: v != null) cfg.settings); sshconf = pkgs.runCommand "sshd.conf-final" { } '' cat ${configFile} - >$out < ''; }; - Macs = mkOption { - type = types.nullOr (types.listOf types.str); + Macs = lib.mkOption { + type = lib.types.nullOr (lib.types.listOf lib.types.str); default = [ "hmac-sha2-512-etm@openssh.com" "hmac-sha2-256-etm@openssh.com" @@ -448,15 +445,15 @@ in ''; }; - StrictModes = mkOption { - type = types.nullOr (types.bool); + StrictModes = lib.mkOption { + type = lib.types.nullOr (lib.types.bool); default = true; description = '' Whether sshd should check file modes and ownership of directories ''; }; - Ciphers = mkOption { - type = types.nullOr (types.listOf types.str); + Ciphers = lib.mkOption { + type = lib.types.nullOr (lib.types.listOf lib.types.str); default = [ "chacha20-poly1305@openssh.com" "aes256-gcm@openssh.com" @@ -474,16 +471,16 @@ in ''; }; - AllowUsers = mkOption { - type = with types; nullOr (listOf str); + AllowUsers = lib.mkOption { + type = with lib.types; nullOr (listOf str); default = null; description = '' If specified, login is allowed only for the listed users. See {manpage}`sshd_config(5)` for details. ''; }; - DenyUsers = mkOption { - type = with types; nullOr (listOf str); + DenyUsers = lib.mkOption { + type = with lib.types; nullOr (listOf str); default = null; description = '' If specified, login is denied for all listed users. Takes @@ -491,8 +488,8 @@ in See {manpage}`sshd_config(5)` for details. ''; }; - AllowGroups = mkOption { - type = with types; nullOr (listOf str); + AllowGroups = lib.mkOption { + type = with lib.types; nullOr (listOf str); default = null; description = '' If specified, login is allowed only for users part of the @@ -500,8 +497,8 @@ in See {manpage}`sshd_config(5)` for details. ''; }; - DenyGroups = mkOption { - type = with types; nullOr (listOf str); + DenyGroups = lib.mkOption { + type = with lib.types; nullOr (listOf str); default = null; description = '' If specified, login is denied for all users part of the listed @@ -512,21 +509,21 @@ in }; # Disabled by default, since pam_motd handles this. PrintMotd = - mkEnableOption "printing /etc/motd when a user logs in interactively" - // { type = types.nullOr types.bool; }; + lib.mkEnableOption "printing /etc/motd when a user logs in interactively" + // { type = lib.types.nullOr lib.types.bool; }; }; }); }; - extraConfig = mkOption { - type = types.lines; + extraConfig = lib.mkOption { + type = lib.types.lines; default = ""; description = "Verbatim contents of {file}`sshd_config`."; }; - moduliFile = mkOption { + moduliFile = lib.mkOption { example = "/etc/my-local-ssh-moduli;"; - type = types.path; + type = lib.types.path; description = '' Path to `moduli` file to install in `/etc/ssh/moduli`. If this option is unset, then @@ -536,8 +533,8 @@ in }; - users.users = mkOption { - type = with types; attrsOf (submodule userOptions); + users.users = lib.mkOption { + type = with lib.types; attrsOf (submodule userOptions); }; }; @@ -545,7 +542,7 @@ in ###### implementation - config = mkIf cfg.enable { + config = lib.mkIf cfg.enable { users.users.sshd = { @@ -555,8 +552,8 @@ in }; users.groups.sshd = {}; - services.openssh.moduliFile = mkDefault "${cfg.package}/etc/ssh/moduli"; - services.openssh.sftpServerExecutable = mkDefault "${cfg.package}/libexec/sftp-server"; + services.openssh.moduliFile = lib.mkDefault "${cfg.package}/etc/ssh/moduli"; + services.openssh.sftpServerExecutable = lib.mkDefault "${cfg.package}/libexec/sftp-server"; environment.etc = authKeysFiles // authPrincipalsFiles // { "ssh/moduli".source = cfg.moduliFile; @@ -567,13 +564,13 @@ in let service = { description = "SSH Daemon"; - wantedBy = optional (!cfg.startWhenNeeded) "multi-user.target"; + wantedBy = lib.optional (!cfg.startWhenNeeded) "multi-user.target"; after = [ "network.target" ]; stopIfChanged = false; path = [ cfg.package pkgs.gawk ]; environment.LD_LIBRARY_PATH = nssModulesPath; - restartTriggers = optionals (!cfg.startWhenNeeded) [ + restartTriggers = lib.optionals (!cfg.startWhenNeeded) [ config.environment.etc."ssh/sshd_config".source ]; @@ -583,7 +580,7 @@ in # socket activation, it goes to the remote side (#19589). exec >&2 - ${flip concatMapStrings cfg.hostKeys (k: '' + ${lib.flip lib.concatMapStrings cfg.hostKeys (k: '' if ! [ -s "${k.path}" ]; then if ! [ -h "${k.path}" ]; then rm -f "${k.path}" @@ -591,10 +588,10 @@ in mkdir -m 0755 -p "$(dirname '${k.path}')" ssh-keygen \ -t "${k.type}" \ - ${optionalString (k ? bits) "-b ${toString k.bits}"} \ - ${optionalString (k ? rounds) "-a ${toString k.rounds}"} \ - ${optionalString (k ? comment) "-C '${k.comment}'"} \ - ${optionalString (k ? openSSHFormat && k.openSSHFormat) "-o"} \ + ${lib.optionalString (k ? bits) "-b ${toString k.bits}"} \ + ${lib.optionalString (k ? rounds) "-a ${toString k.rounds}"} \ + ${lib.optionalString (k ? comment) "-C '${k.comment}'"} \ + ${lib.optionalString (k ? openSSHFormat && k.openSSHFormat) "-o"} \ -f "${k.path}" \ -N "" fi @@ -603,8 +600,8 @@ in serviceConfig = { ExecStart = - (optionalString cfg.startWhenNeeded "-") + - "${cfg.package}/bin/sshd " + (optionalString cfg.startWhenNeeded "-i ") + + (lib.optionalString cfg.startWhenNeeded "-") + + "${cfg.package}/bin/sshd " + (lib.optionalString cfg.startWhenNeeded "-i ") + "-D " + # don't detach into a daemon process "-f /etc/ssh/sshd_config"; KillMode = "process"; @@ -625,7 +622,7 @@ in { description = "SSH Socket"; wantedBy = [ "sockets.target" ]; socketConfig.ListenStream = if cfg.listenAddresses != [] then - concatMap + lib.concatMap ({ addr, port }: if port != null then [ "${addr}:${toString port}" ] else map (p: "${addr}:${toString p}") cfg.ports) @@ -645,7 +642,7 @@ in }; - networking.firewall.allowedTCPPorts = optionals cfg.openFirewall cfg.ports; + networking.firewall.allowedTCPPorts = lib.optionals cfg.openFirewall cfg.ports; security.pam.services.sshd = lib.mkIf cfg.settings.UsePAM { startSession = true; @@ -662,34 +659,34 @@ in services.openssh.authorizedKeysFiles = lib.optional cfg.authorizedKeysInHomedir "%h/.ssh/authorized_keys" ++ [ "/etc/ssh/authorized_keys.d/%u" ]; - services.openssh.settings.AuthorizedPrincipalsFile = mkIf (authPrincipalsFiles != {}) "/etc/ssh/authorized_principals.d/%u"; + services.openssh.settings.AuthorizedPrincipalsFile = lib.mkIf (authPrincipalsFiles != {}) "/etc/ssh/authorized_principals.d/%u"; - services.openssh.extraConfig = mkOrder 0 + services.openssh.extraConfig = lib.mkOrder 0 '' Banner ${if cfg.banner == null then "none" else pkgs.writeText "ssh_banner" cfg.banner} AddressFamily ${if config.networking.enableIPv6 then "any" else "inet"} - ${concatMapStrings (port: '' + ${lib.concatMapStrings (port: '' Port ${toString port} '') cfg.ports} - ${concatMapStrings ({ port, addr, ... }: '' - ListenAddress ${addr}${optionalString (port != null) (":" + toString port)} + ${lib.concatMapStrings ({ port, addr, ... }: '' + ListenAddress ${addr}${lib.optionalString (port != null) (":" + toString port)} '') cfg.listenAddresses} - ${optionalString cfgc.setXAuthLocation '' + ${lib.optionalString cfgc.setXAuthLocation '' XAuthLocation ${pkgs.xorg.xauth}/bin/xauth ''} - ${optionalString cfg.allowSFTP '' - Subsystem sftp ${cfg.sftpServerExecutable} ${concatStringsSep " " cfg.sftpFlags} + ${lib.optionalString cfg.allowSFTP '' + Subsystem sftp ${cfg.sftpServerExecutable} ${lib.concatStringsSep " " cfg.sftpFlags} ''} AuthorizedKeysFile ${toString cfg.authorizedKeysFiles} - ${optionalString (cfg.authorizedKeysCommand != "none") '' + ${lib.optionalString (cfg.authorizedKeysCommand != "none") '' AuthorizedKeysCommand ${cfg.authorizedKeysCommand} AuthorizedKeysCommandUser ${cfg.authorizedKeysCommandUser} ''} - ${flip concatMapStrings cfg.hostKeys (k: '' + ${lib.flip lib.concatMapStrings cfg.hostKeys (k: '' HostKey ${k.path} '')} ''; @@ -699,13 +696,13 @@ in { nativeBuildInputs = [ validationPackage ]; } '' - ${concatMapStringsSep "\n" + ${lib.concatMapStringsSep "\n" (lport: "sshd -G -T -C lport=${toString lport} -f ${sshconf} > /dev/null") cfg.ports} - ${concatMapStringsSep "\n" + ${lib.concatMapStringsSep "\n" (la: - concatMapStringsSep "\n" - (port: "sshd -G -T -C ${escapeShellArg "laddr=${la.addr},lport=${toString port}"} -f ${sshconf} > /dev/null") + lib.concatMapStringsSep "\n" + (port: "sshd -G -T -C ${lib.escapeShellArg "laddr=${la.addr},lport=${toString port}"} -f ${sshconf} > /dev/null") (if la.port != null then [ la.port ] else cfg.ports) ) cfg.listenAddresses} @@ -726,7 +723,7 @@ in # Grab the groups, we don't care about the group identifiers lib.attrValues ( # Group the settings that are the same in lower case - lib.groupBy lib.strings.toLower (attrNames cfg.settings) + lib.groupBy lib.strings.toLower (lib.attrNames cfg.settings) ) ); formattedDuplicates = lib.concatMapStringsSep ", " (dupl: "(${lib.concatStringsSep ", " dupl})") duplicates; @@ -735,7 +732,7 @@ in assertion = lib.length duplicates == 0; message = ''Duplicate sshd config key; does your capitalization match the option's? Duplicate keys: ${formattedDuplicates}''; })] - ++ forEach cfg.listenAddresses ({ addr, ... }: { + ++ lib.forEach cfg.listenAddresses ({ addr, ... }: { assertion = addr != null; message = "addr must be specified in each listenAddresses entry"; });