Shorthands for frequently used config values

I find myself often adding stuff like this at the beginning of my role files in NixOS configuration:

  fqdn = with config.networking; "${hostName}.${domain}";
in {

Which is quite a mouthful if you want to use the host fqdn in multiple places. How would I go about making this more easily available in my nixos config files?

One way I came up with was to add an overlay with fqdn in it:

{ config, ... }:

  nixpkgs.overlays = [
    (self: super: {
      fqdn = with config.networking; "${hostName}.${domain}";

This allows me to access it via pkgs.fqdn in any file, but it does seem a bit awkward, as that kind of information should not live under pkgs. Is there a better way to achieve this?

1 Like

“extend” the networking module to also contain the FQDN.

{ config, lib }:

  options.networking.fqdn = lib.mkOption { ... };

  config.networking.fqdn = with config.networking; "${hostName}.${domain}";

Something like that (typed from a mobile)


I think the “lib” module is intended for global variables, e.g. :

{ config, ... } : {
   lib.vars.fqdn = with config.networking; "${hostName}.${domain}";

After that you could refer to config.lib.vars.fqdn

@Ninlives That doesn’t seem to work. As far as I can tell there is no such thing as lib.vars.

error: attribute 'vars' missing, at /etc/nixos/roles/something.nix:46:36

@NobbZ That’s better than my overlay solution, thanks!

In theory you could even move the assignment to default of the mkOption, and override the FQDN elsewhere in a hosts config if necessary for some reason.

Have your imported the first file which contains the definition?

This is what I get when I try your suggested method.

error: attribute 'vars' missing, at /etc/nixos/roles/something.nix:47:36