9
submitted 10 months ago by gomp@lemmy.ml to c/nixos@lemmy.ml

I have an option that must be left with the default value when a certain flag (another option) is false.

I didn't find any example (let alone documentation) on how to implement this, so I've come up with two ideas:

option-that-errors-out-if-set-when-flag-is-false =
let
  default = if config.some-flag
          then "some default value for when flag is true"
          else "value that should not be changed when flag is false";
in lib.mkOption {
  type = lib.types.str;
  inherit default;
  apply = v: assert assertMsg (config.some-flag || v == default) "Do not set this option unless 'flag' is true";
          v;
};
option-that-ignores-value-when-flag-is-false =
let
  default = if config.some-flag
          then "some default value for when flag is true"
          else "value that should not be changed when flag is false";
in lib.mkOption {
  type = lib.types.str;
  inherit default;
  apply = v: if config.some-flag then v else default;
};

Which one do you think is "best" (cleaner, more idiomatic, etc..)?

Is apply the "right" place to validate options? Should I make a custom type instead? Should I approach this in some different way?

top 1 comments
sorted by: hot top controversial new old
[-] ck_@discuss.tchncs.de 5 points 10 months ago

You probability want to use assertions

this post was submitted on 16 Sep 2023
9 points (100.0% liked)

nixos

1208 readers
14 users here now

All about NixOS - https://nixos.org/

founded 4 years ago