How to abort with in MkMerge?

I am writing a nix module, and I want to abort and throw with error message, if the user hasn’t enabled that option.

# leaving out option here...

  config = (mkMerge [
    (mkIf (not cfg.windowManager.enable) abort
      (config.lib.custom.addErrPrefix "You haven't defined any window manager."))
      
    ((mkIf cfg.windowManager.enable) {
      services.xserver = {
        windowManager.leftwm = { enable = true; };
        displayManager = {
          defaultSession = "none+${cfg.windowManager.package.pname}";
        };
      };
      home-manager.users.hugosum = let
        terminal = cfg.emulator.package;
        path = (config.lib.custom.getExecPath terminal);
      in {
        home.packages = [ cfg.shell.package ];

        xdg.configFile = mkMerge [
          (mkIf (cfg.windowManager.package.pname == "leftwm") {
            "leftwm/config.toml" = {
              source = (pkgs.writeText "leftwm-config"
                (config.lib.custom.hydratePlaceholder [ "#dotfile-manager" ]
                  [ "'${path}'" ] cfg.windowManager.configPath));
            };
          })
        ];
      };
    })
  ]);

But this can’t build correctly, and I got the error attempt to call something which is not a function but a set. I then guess I have to return something there.

    (if (cfg.windowManager.enable) then
      abort
      (config.lib.custom.addErrPrefix "You haven't defined any window manager.")
    else
      { })

But this wont’ work either, which would hit infinite recurison, as stated in this post: Using mkIf with nested if

How can I abort/throw with in mkMerge? Or should I do it somewhere else?

Use assertions

2 Likes