agetty: Add autologinUser config option

This option causes the specified user to be automatically logged in at
the virtual console.

While at it, refactor and make a helper function for building the getty
command line.

+17 -6
+17 -6
nixos/modules/services/ttys/agetty.nix
··· 10 11 services.mingetty = { 12 13 greetingLine = mkOption { 14 type = types.str; 15 default = ''<<< Welcome to NixOS ${config.system.nixosVersion} (\m) - \l >>>''; ··· 46 47 ###### implementation 48 49 - config = { 50 - 51 systemd.services."getty@" = 52 - { serviceConfig.ExecStart = "@${pkgs.utillinux}/sbin/agetty agetty --noclear --login-program ${pkgs.shadow}/bin/login --keep-baud %I 115200,38400,9600 $TERM"; 53 restartIfChanged = false; 54 }; 55 56 systemd.services."serial-getty@" = 57 { serviceConfig.ExecStart = 58 let speeds = concatStringsSep "," (map toString config.services.mingetty.serialSpeed); 59 - in "@${pkgs.utillinux}/sbin/agetty agetty --login-program ${pkgs.shadow}/bin/login %I ${speeds} $TERM"; 60 restartIfChanged = false; 61 }; 62 63 systemd.services."container-getty@" = 64 { unitConfig.ConditionPathExists = "/dev/pts/%I"; # Work around being respawned when "machinectl login" exits. 65 - serviceConfig.ExecStart = "@${pkgs.utillinux}/sbin/agetty agetty --noclear --login-program ${pkgs.shadow}/bin/login --keep-baud pts/%I 115200,38400,9600 $TERM"; 66 restartIfChanged = false; 67 }; 68 69 systemd.services."console-getty" = 70 - { serviceConfig.ExecStart = "@${pkgs.utillinux}/sbin/agetty agetty --noclear --login-program ${pkgs.shadow}/bin/login --keep-baud console 115200,38400,9600 $TERM"; 71 serviceConfig.Restart = "always"; 72 restartIfChanged = false; 73 enable = mkDefault config.boot.isContainer;
··· 10 11 services.mingetty = { 12 13 + autologinUser = mkOption { 14 + type = types.nullOr types.str; 15 + default = null; 16 + description = '' 17 + Username of the account that will be automatically logged in at the console. 18 + If unspecified, a login prompt is shown as usual. 19 + ''; 20 + }; 21 + 22 greetingLine = mkOption { 23 type = types.str; 24 default = ''<<< Welcome to NixOS ${config.system.nixosVersion} (\m) - \l >>>''; ··· 55 56 ###### implementation 57 58 + config = let 59 + autologinArg = optionalString (config.services.mingetty.autologinUser != null) "--autologin ${config.services.mingetty.autologinUser}"; 60 + gettyCmd = extraArgs: "@${pkgs.utillinux}/sbin/agetty agetty --login-program ${pkgs.shadow}/bin/login ${autologinArg} ${extraArgs}"; 61 + in { 62 systemd.services."getty@" = 63 + { serviceConfig.ExecStart = gettyCmd "--noclear --keep-baud %I 115200,38400,9600 $TERM"; 64 restartIfChanged = false; 65 }; 66 67 systemd.services."serial-getty@" = 68 { serviceConfig.ExecStart = 69 let speeds = concatStringsSep "," (map toString config.services.mingetty.serialSpeed); 70 + in gettyCmd "%I ${speeds} $TERM"; 71 restartIfChanged = false; 72 }; 73 74 systemd.services."container-getty@" = 75 { unitConfig.ConditionPathExists = "/dev/pts/%I"; # Work around being respawned when "machinectl login" exits. 76 + serviceConfig.ExecStart = gettyCmd "--noclear --keep-baud pts/%I 115200,38400,9600 $TERM"; 77 restartIfChanged = false; 78 }; 79 80 systemd.services."console-getty" = 81 + { serviceConfig.ExecStart = gettyCmd "--noclear --keep-baud console 115200,38400,9600 $TERM"; 82 serviceConfig.Restart = "always"; 83 restartIfChanged = false; 84 enable = mkDefault config.boot.isContainer;