Created
December 31, 2024 13:17
-
-
Save isabelroses/13e911ecfbcc8c164ae533ed1989d063 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let | |
budgetIsDefined = value: (builtins.tryEval value).success; | |
in | |
import <nixpkgs/nixos> { | |
configuration = | |
{ config, lib, ... }: | |
{ | |
imports = [ | |
{ | |
options = { | |
# Create a submodule option | |
aSubmodule = lib.mkOption { | |
type = lib.types.submodule { | |
options = { | |
# With an inner option | |
someSubmoduleOption = lib.mkEnableOption "something" // { | |
# Add some warning stuff | |
description = "Alias of `aRegularOption`"; | |
apply = lib.trace "Obsolete option `aSubmodule.someSubmoduleOption` is used. It was renamed to `aRegularOption`."; | |
visible = false; # Do you need more than this to hide it from the docs? Dunno | |
}; | |
}; | |
}; | |
default = { }; | |
}; | |
# Then add a top-level option | |
aRegularOption = lib.mkEnableOption "something"; | |
}; | |
} | |
# Do the magic alias part | |
{ | |
# We only want to do any of this if the alias option is actually used | |
config = lib.mkIf (budgetIsDefined config.aSubmodule.someSubmoduleOption) { | |
# Place the warning `mkRenamedOptionModule` normally would | |
warnings = [ | |
"The option `aSubmodule.someSubmoduleOption` has been renamed to `someRegularOption`." | |
]; | |
# Set the new option to the old one | |
# This will create a conflict is both are used, again like `mkRenamedOptionModule` | |
# It will also maintain the trace set in the `apply` above :) | |
aRegularOption = config.aSubmodule.someSubmoduleOption; | |
}; | |
} | |
{ | |
# Now test the alias | |
# `nix eval --file repro.nix config.aRegularOption` | |
aSubmodule.someSubmoduleOption = true; | |
} | |
]; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment