Occasionally, NixOS changes config options in a way that is not entirely backwards compatible. For example, nixos 19.09 did not have a programs.gnupg.agent.pinentryFlavor option, but in nixos unstable (soon 20.03) I need to set it to a non-default value in order to get the right pinentry variant.

I share my configuration across machines, some of which run nixos-19.09 and some nixos-unstable, so I want my configuration to be compatible with both. (even without multiple machines, it would be nice to be able to switch nixos channels without breakage)

Setting programs.gnupg.agent.pinentryFlavor = "gtk2"; as needed for nixos-unstable causes nixos-rebuild to fail on nixos-19.09:

error: The option `programs.gnupg.agent.pinentryFlavor' defined in `[...]/desktop.nix' does not exist.
(use '--show-trace' to show detailed location information)

Is there a way to check if an option is valid?

Essentially, I'm looking for what to write in place of ???(pinentryFlavor)) here, so as to not set a nonexistent option:

programs.gnupg.agent = { enable = true;} // (
  if ???(pinentryFlavor)
  then { pinentryFlavor = "gtk2"; }
  else {});

1 Answer 1


The configuration function does receive an options attr, so it is possible to check if a given option is defined using builtins.hasAttr before setting it in the configuration.

Most NixOS configurations don't extract options, so you may need to add it first. For example:

{ config, pkgs, options, ... }:
  programs.gnupg.agent =
    { enable = true;  } //
    # False on NixOS 19.09
    (if builtins.hasAttr "pinentryFlavor" options.programs.gnupg.agent
     then { pinentryFlavor = "gtk2"; }
     else {});

Similarly, the same approach can be used to set options used by nixos-rebuild build-vm, which would normally not be available.

Instead of needing to set options via environment variables when running the VM like

QEMU_OPTS='-m 4096 -smp 4 -soundhw ac97' ./result/bin/run-*-vm

the equivalent options can be set in configuration.nix:

  # The default 384MB RAM is not enough to run Firefox in a VM
  virtualisation =
    lib.optionalAttrs (builtins.hasAttr "qemu" options.virtualisation) {
      memorySize = 4096;
      cores = 4;
      qemu.options = [ "-soundhw ac97" ];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.