Skip to content

Instantly share code, notes, and snippets.

@caadar
Last active October 23, 2023 00:34
Show Gist options
  • Save caadar/7884b1bf16cb1fc2c7cde33d329ae37f to your computer and use it in GitHub Desktop.
Save caadar/7884b1bf16cb1fc2c7cde33d329ae37f to your computer and use it in GitHub Desktop.
How to make a user autologin at the console on NixOS

Autologin a user at the console (NixOS)

Problem

NixOS provide services.mingetty.autologinUser option, but it can’t be used for the specific TTY.

Minimal working example

TTY1 and user “guest”:

systemd.services."autovt@tty1".description = "Autologin at the TTY1";
systemd.services."autovt@tty1".after = [ "systemd-logind.service" ];  # without it user session not started and xorg can't be run from this tty
systemd.services."autovt@tty1".wantedBy = [ "multi-user.target" ];
systemd.services."autovt@tty1".serviceConfig =
  { ExecStart = [
      ""  # override upstream default with an empty ExecStart
      "@${pkgs.utillinux}/sbin/agetty agetty --login-program ${pkgs.shadow}/bin/login --autologin guest --noclear %I $TERM"
    ];
    Restart = "always";
    Type = "idle";
  };

Complete solution

TTY1 and user “guest”:

systemd.targets."autologin-tty1".description = "Final";
systemd.targets."autologin-tty1".requires = [ "multi-user.target" ];
systemd.targets."autologin-tty1".after = [ "multi-user.target" ];
systemd.targets."autologin-tty1".unitConfig = { AllowIsolate="yes"; };

systemd.services."supress-kernel-logging".description = "Suppress kernel logging to the console";
systemd.services."supress-kernel-logging".after = [ "multi-user.target" ];
systemd.services."supress-kernel-logging".wantedBy = [ "autologin-tty1.target" ];
systemd.services."supress-kernel-logging".serviceConfig =
  { ExecStart = [
      "${pkgs.utillinux}/sbin/dmesg -n 1"
    ];
    Type = "oneshot";
  };

systemd.services."autovt@tty1".description = "Autologin on TTY1";
systemd.services."autovt@tty1".after = [ "supress-kernel-logging.service" ];
systemd.services."autovt@tty1".wantedBy = [ "autologin-tty1.target" ];
systemd.services."autovt@tty1".restartIfChanged = false;
systemd.services."autovt@tty1".serviceConfig =
  { ExecStart = [
      ""  # override upstream default with an empty ExecStart
      "@${pkgs.utillinux}/sbin/agetty agetty --login-program ${pkgs.shadow}/bin/login --autologin guest --noclear %I $TERM"
    ];
    Restart = "always";
    Type = "idle";
  };

Some useful links

https://github.com/a-irs/infra/blob/f73bf14e654df1e0ad9b4a5c1e5c72d0649ac5d6/nixos/etc/conf.d/workstation.nix#L12 https://github.com/coreyoconnor/nix_configs/blob/9aeb6f0ad4a707f0d3f89177774a532c880d3b6a/status-tty.nix#L23 https://web.archive.org/web/20160325165924/https://www.mauras.ch/systemd-run-it-last.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment