summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/ttys/getty.nix
blob: 226391d6731f72574d096dedae692c7f11b54a04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
{
  config,
  lib,
  pkgs,
  ...
}:

with lib;

let
  cfg = config.services.getty;

  baseArgs = [
    "--login-program"
    "${cfg.loginProgram}"
    "--issue-file"
    "/etc/issue:/etc/issue.d:/run/issue:/run/issue.d"
  ]
  ++ optionals (cfg.autologinUser != null && !cfg.autologinOnce) [
    "--autologin"
    cfg.autologinUser
  ]
  ++ optionals (cfg.loginOptions != null) [
    "--login-options"
    cfg.loginOptions
  ]
  ++ cfg.extraArgs;

  gettyCmd = args: "${lib.getExe' pkgs.util-linux "agetty"} ${escapeShellArgs baseArgs} ${args}";

  autologinOnceScript =
    otherArgs:
    pkgs.writeShellApplication {
      name = "autologin-once";
      text = ''
        autologged="/run/agetty.autologged"
        if test "$TTY" = tty1 && ! test -f "$autologged"; then
          touch "$autologged"
          exec ${gettyCmd "${otherArgs} --autologin ${cfg.autologinUser}"}
        fi
        exec ${gettyCmd otherArgs}
      '';
    };

in

{

  ###### interface

  imports = [
    (mkRenamedOptionModule [ "services" "mingetty" ] [ "services" "getty" ])
    (mkRemovedOptionModule [ "services" "getty" "serialSpeed" ]
      ''set non-standard baudrates with `boot.kernelParams` i.e. boot.kernelParams = ["console=ttyS2,1500000"];''
    )
  ];

  options = {

    services.getty = {

      autologinUser = mkOption {
        type = types.nullOr types.str;
        default = null;
        description = ''
          Username of the account that will be automatically logged in at the console.
          If unspecified, a login prompt is shown as usual.
        '';
      };

      autologinOnce = mkOption {
        type = types.bool;
        default = false;
        description = ''
          If enabled the automatic login will only happen in the first tty
          once per boot. This can be useful to avoid retyping the account
          password on systems with full disk encrypted.
        '';
      };

      enable = mkOption {
        type = types.bool;
        default = true;
        description = ''
          Include getty in the system.

          getty is quiescent until called into action and does not have
          runtime costs if it is not used. The benefit of disabling it is
          in reducing closure size.

          Disabling getty means that console login may not be possible,
          `machinectl shell` and `login` may not work, and other ills.
          It is only recommended for lights-out, headless containers,
          appliances, and similar configurations not meant for any human
          interaction ever.
        '';
      };

      loginProgram = mkOption {
        type = types.path;
        default = "${pkgs.shadow}/bin/login";
        defaultText = literalExpression ''"''${pkgs.shadow}/bin/login"'';
        description = ''
          Path to the login binary executed by agetty.
        '';
      };

      loginOptions = mkOption {
        type = types.nullOr types.str;
        default = null;
        description = ''
          Template for arguments to be passed to
          {manpage}`login(1)`.

          See {manpage}`agetty(1)` for details,
          including security considerations.  If unspecified, agetty
          will not be invoked with a {option}`--login-options`
          option.
        '';
        example = "-h darkstar -- \\u";
      };

      extraArgs = mkOption {
        type = types.listOf types.str;
        default = [ ];
        description = ''
          Additional arguments passed to agetty.
        '';
        example = [ "--nohostname" ];
      };

      greetingLine = mkOption {
        type = types.str;
        description = ''
          Welcome line printed by agetty.
          The default shows current NixOS version label, machine type and tty.
        '';
      };

      helpLine = mkOption {
        type = types.lines;
        default = "";
        description = ''
          Help line printed by agetty below the welcome line.
          Used by the installation CD to give some hints on
          how to proceed.
        '';
      };

    };

  };

  ###### implementation

  config = mkIf cfg.enable {
    # Note: this is set here rather than up there so that changing
    # nixos.label would not rebuild manual pages
    services.getty.greetingLine = mkDefault ''<<< Welcome to ${config.system.nixos.distroName} ${config.system.nixos.label} (\m) - \l >>>'';
    services.getty.helpLine = mkIf (
      config.documentation.nixos.enable && config.documentation.doc.enable
    ) "\nRun 'nixos-help' for the NixOS manual.";

    systemd.additionalUpstreamSystemUnits = [
      "getty.target"
      "getty-pre.target"
      "getty@.service"
      "serial-getty@.service"
      "console-getty.service"
      "container-getty@.service"
    ];

    # We can't just rely on 'Conflicts=autovt@tty1.service' because
    # 'switch-to-configuration switch' will start 'autovt@tty1.service'
    # and kill the display manager.
    systemd.targets.getty.wants = lib.mkIf (!config.services.displayManager.enable) [
      "autovt@tty1.service"
    ];

    systemd.services."getty@" = {
      serviceConfig.ExecStart = [
        # override upstream default with an empty ExecStart
        ""
        (
          if cfg.autologinOnce then
            lib.getExe (autologinOnceScript ''--noclear --keep-baud "$TTY" 115200,38400,9600 "$TERM"'')
          else
            gettyCmd "--noclear --keep-baud %I 115200,38400,9600 $TERM"
        )
      ];
      environment.TTY = "%I";
      restartIfChanged = false;
      # logind hardcodes spawning autovt@ttyN.service on VT switch. Upstream
      # declares this alias via [Install] Alias=, which NixOS does not process.
      aliases = [ "autovt@.service" ];
    };

    systemd.services."serial-getty@" = {
      serviceConfig.ExecStart = [
        "" # override upstream default with an empty ExecStart
        (gettyCmd "%I --keep-baud $TERM")
      ];
      restartIfChanged = false;
    };

    systemd.services."container-getty@" = {
      serviceConfig.ExecStart = [
        "" # override upstream default with an empty ExecStart
        (gettyCmd "--noclear --keep-baud pts/%I 115200,38400,9600 $TERM")
      ];
      restartIfChanged = false;
    };

    systemd.services.console-getty = {
      serviceConfig.ExecStart = [
        "" # override upstream default with an empty ExecStart
        (gettyCmd "--noclear --keep-baud console 115200,38400,9600 $TERM")
      ];
      serviceConfig.Restart = "always";
      restartIfChanged = false;
      enable = mkDefault config.boot.isContainer;
    };

    environment.etc.issue = mkDefault {
      # Friendly greeting on the virtual consoles.
      source = pkgs.writeText "issue" ''

        ${config.services.getty.greetingLine}
        ${config.services.getty.helpLine}

      '';
    };

  };

  meta.maintainers = with maintainers; [ RossComputerGuy ];
}