diff --git a/modules/machine-info.nix b/modules/machine-info.nix new file mode 100644 index 0000000..a30f2a1 --- /dev/null +++ b/modules/machine-info.nix @@ -0,0 +1,119 @@ +{ config, pkgs, lib, ... }@nixpkgs: with lib; { + options.environment.machineInfo = mkOption { + description = lib.mdDoc '' + Machine metadata, including stylized hostname, computer icon, etc. + + This module controls the options written to `/etc/machine-info`. For more + information, see [the freedesktop documentation][1]. + + [1]: https://www.freedesktop.org/software/systemd/man/machine-info.html + ''; + default = {}; + type = types.submodule { options = { + + prettyHostname = mkOption { + description = lib.mdDoc '' + A pretty, human-readable hostname for this machine, potentially including + spaces, unicode, and emoji. If unset, this falls back to the network hostname + set in `networking.hostName`. + ''; + type = with types; nullOr str; + default = null; + defaultText = literalExpression "null"; + example = literalExpression "\"Jade's Laptop 💎\""; + }; + + iconName = mkOption { + description = lib.mdDoc '' + An XDG icon which should be associated with this machine. Some common choices + include: `"computer"`, `"phone"`, but a complete list of icons can be found in + the [XDG Icon Naming Spec][1]. + + If left unset, applications will typically default to `"computer"`. + + [1]: https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html + ''; + type = with types; nullOr str; + default = null; + defaultText = literalExpression "null"; + example = literalExpression "\"computer\""; + }; + + chassis = mkOption { + description = lib.mdDoc '' + The type of chassis this machine resides within. This is typically detected + automatically, but can be manually overridden here. + ''; + type = with types; nullOr (enum [ + "desktop" + "laptop" + "convertible" + "server" + "tablet" + "handset" + "watch" + "embedded" + "vm" + "container" + ]); + default = null; + defaultText = literalExpression "null"; + example = literalExpression "\"server\""; + }; + + deployment = mkOption { + description = lib.mdDoc '' + If this machine is part of a deployment environment / pipeline, this option can + be used to specify what environment/pipeline stage it manages. + + Typically, but not necessarily, set to something like `"development"`, + `"integration"`, `"staging"`, or `"production"`. + ''; + type = with types; nullOr str; + default = null; + defaultText = literalExpression "null"; + example = literalExpression "\"production\""; + }; + + location = mkOption { + description = lib.mdDoc '' + A human-readable short description of the location of this machine. + + This can be set to whatever has the most meaning for you, for example "Living + Room", "Left Rack, 2nd Shelf", or "Parishville, NY". + ''; + type = with types; nullOr str; + default = null; + defaultText = literalExpression "null"; + example = literalExpression "\"Bedroom\""; + }; + + extraOptions = mkOption { + description = lib.mdDoc '' + Extra variables to put in `/etc/machine-info` + ''; + type = with types; attrsOf str; + default = {}; + defaultText = literalExpression "{ }"; + example = literalExpression "{ HARDWARE_VENDOR = \"Intel Corp.\" }"; + }; + + };}; + }; + + config.environment.etc.machine-info = + with config.environment.machineInfo; + let + rawShellVars = { + PRETTY_HOSTNAME = prettyHostname; + ICON_NAME = iconName; + CHASSIS = chassis; + DEPLOYMENT = deployment; + LOCATION = location; + } // extraOptions; + nonNullShellVars = attrsets.filterAttrs (k: v: v != null) rawShellVars; + in rec { + text = strings.toShellVars nonNullShellVars; + enable = builtins.stringLength text > 0; + }; +} \ No newline at end of file diff --git a/nixos.nix b/nixos.nix index db797d5..d498280 100644 --- a/nixos.nix +++ b/nixos.nix @@ -10,6 +10,7 @@ let systemInformation = import ./system/system-information.nix; in [ ./configs/filesystems.nix ./system/hardware-requirements.nix + ./modules/machine-info.nix ]; @@ -166,9 +167,7 @@ let systemInformation = import ./system/system-information.nix; in }; }; - environment.etc.machine-info.text = lib.strings.toShellVars { - PRETTY_HOSTNAME = systemInformation.prettyHostname; - }; + environment.machineInfo.prettyHostname = systemInformation.prettyHostname; # Enable the OpenSSH daemon. # services.openssh.enable = true;