!wfudwzqQUiJYJnqfSY:nixos.org

NixOS Module System

172 Members
36 Servers

You have reached the beginning of time (for this room).


SenderMessageTime
13 Dec 2024
@getchoo:matrix.orggetchooThanks for looking into it and the explanation. Maybe it's something I'll look into working on once I'm more familiar with some of the internals here This was actually my first time looking at the impl of stuff like doRename 😆22:59:21
14 Dec 2024
@mattsturg:matrix.orgMatt Sturgeon

Don't rename modules usually expect the alias-option to not exist; i.e. doRename is responsible for creating the other option ? Even if so, I think that's unrelated.

I've definitely made renames into submodules from outside before (we do this for 50% of nixvim lol), but IIRC making renames out from submodules runs into issues because options.** doesn't exist. Specifically, you run into a _type = "option" object at the submodule-option boundary, options.aSubmodule in the above example.

In nixvim I experimented with resolving the option path recursively, using type.getSubOptions. That was for removal modules, but similar principal.

(This would be (options.aSubmodule._type.getSubOptions options.aSubmodule.loc).someSubmoduleOption in the prior example)

It's also been a while since I looked at the linked code, and IIRC some stuff in that file is broken. Probably the unrelated tryEval hack 😅

04:48:16
17 Dec 2024
@getchoo:matrix.orggetchoo

Came back to the problem tonight before checking this room again. Seems I came up with a pretty similar solution lol

# repro.nix
let
  lib = import <nixpkgs/lib>;

  budgetIsDefined = value: (builtins.tryEval value).success;
in

lib.evalModules {
  modules = [
    {
      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;
              };
            };
          };

          default = { };
        };

        # Then add a top-level option
        aRegularOption = lib.mkEnableOption "something";
      };
    }

    # Do the actual aliasing
    (
      { config, ... }:
      {
        # We only want to do any of this if the alias option is used
        config = lib.mkIf (budgetIsDefined config.aSubmodule.someSubmoduleOption) {
          aRegularOption = config.aSubmodule.someSubmoduleOption;
        };
      }
    )

    {
      # Now test the alias
      # `nix eval --file repro.nix config.aRegularOption`
      aSubmodule.someSubmoduleOption = true;
    }
  ];
}

The tryEval hack also has a bit of a footgun with type checking though, as incorrect definition errors are eaten up in the same way as when there are no definitions. Don't think there is any way to get more context out of primitive throws though, so this will probably need to be good enough ™️

02:01:44
@getchoo:matrix.orggetchooAnd seriously, thanks for linking that deprecations file. If I ever need to do this more, I'll totally have to borrow some stuff from there 👍️02:03:28
@mattsturg:matrix.orgMatt Sturgeon

I really don't recommend using tryEval in this way... It was very situational in nixvim, and even then still probably wasn't the best solution. As you've pointed out, it has plenty of downsides.

I would recommend trying to find the option by recursively walking the "option attr path" (aka loc), checking each attr for lib.isOption and if true, continuing with opt.type.getSubOptions opt.loc.

getOptionRecursive was the part of the file I was hoping you would take inspiration from 😅

Once you have the actual (sub)option you can make use of opt.isDefined with no hacks needed.

09:49:16
20 Dec 2024
@xiaoxiangmoe:matrix.org🐰 xiaoxiangmoe joined the room.13:59:03
21 Dec 2024
@stablejoy:matrix.orgstablejoy left the room.05:08:18
@sleepymonad:matrix.orgsleepymonad joined the room.21:16:14
@nam3l33ss:matrix.org·☽•Nameless☆•777 · ± changed their profile picture.21:37:38
@sleepymonad:matrix.orgsleepymonad set a profile picture.21:56:33
22 Dec 2024
@allrealmsoflife:matrix.orgallrealmsoflife joined the room.20:27:50

Show newer messages


Back to Room ListRoom Version: 10