Skip to content

Instantly share code, notes, and snippets.

@exarkun
Created June 19, 2023 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exarkun/6386b35463daa8445ae5e3b32818d8fe to your computer and use it in GitHub Desktop.
Save exarkun/6386b35463daa8445ae5e3b32818d8fe to your computer and use it in GitHub Desktop.
distinctTTY = name:
let
# Does this systemd service have a TTY?
hasTTY = service: (service.serviceConfig.TTYPath or null) != null;
# Get only the systemd services that have a TTY.
ttyServices = self: services: lib.filterAttrs (name: value: name == self || hasTTY value) services;
# Get the names of some systemd services with a stable ordering.
serviceNames = services: builtins.sort (a: b: a < b) (builtins.attrNames services);
# Get a stable ordinal for a named service in a collection of services.
servicePosition = name: services:
lib.findSingle
(s: s == name)
(builtins.error "Could not find service")
(builtins.error "Found multiple services sharing a name")
(serviceNames (ttyServices name services));
num = servicePosition name config.systemd.services;
in
"/dev/tty${num}";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment