Conditionally change list values

I have this simple module, that for some reason doesn’t work. If I remove the conditional (if isDarwin) it works fine. But if I add the if statement back it doesn’t work. I have no idea why, the code seems correct for me & I have other modules with similar structures (without conditionals though) & they are working fine.

My setup is using flakes, nix-darwin & home-manager & I’m very much a beginner so I’m sure I’m doing lots of things wrong. My full setup is here it’s heavily WIP.

{ pkgs, lib, config, ... }:

with config.settings;

let

  cfg = config.my.gui;

in {
  options = with lib; {
    my.gui = {
      enable = mkEnableOption ''
        Whether to enable gui module
      '';
    };
  };

  config = with lib;
    mkIf cfg.enable {
      users.users.${username} = {
        packages = with pkgs;
          [
            slack
          ] ++ (if pkgs.stdenv.isDarwin then
            [ ]
          else [
            brave
            firefox
            obsidian
            zoom-us
          ]);
      };
    };
}

If I remove the conditional ( if isDarwin ) it works fine

do you mind pasting the error? Not sure if flakes is complaining about impure eval (which it shouldn’t, as your system should have been passed at this point), or something else.

(if pkgs.stdenv.isDarwin then
            [ ]
          else [
            brave
            firefox
            obsidian
            zoom-us
          ]

could be:

  lib.optionals (!pkgs.stdenv.isDarwin) [
            brave
            firefox
            obsidian
            zoom-us
          ]

There is no error, that’s the issue & nix build completes fine. But none of these packages gets installed, for example, Slack should be installed because it’s outside of the if/else. But it doesn’t get installed. If I remove the if/else it gets installed.

the Darwin list is empty for now, but I plan to add stuff there. So that’s why I need the if/else.

I’m not sure, seems like it should be right since you specified system here: https://github.com/ahmedelgabri/dotfiles/blob/7e1bd6644be7160842985b893908ea782d7e4fec/flake.nix#L112. I would just verify that stdenv.isDarwin is giving you what want.

Ok, it looks like it installs Slack but doesn’t symlink it to ~/Applications because I found Slack.app in the store… I also tested the isDarwin by add [ hello ] there & that was installed properly.

So it seems that the condition is fine, but the symlinking is not working for some reason… I confirmed that by adding one of my own to this list & indeed the symlink was removed from ~/Application

I’m really puzzled by the behaviour of linking Apps. Here is another module, very simple & straight forward & has no conditionals even. Same problem. The mpv.app, gets installed but doesn’t get linked…

Ok, I narrowed it down to this.

Not sure exactly why this is happening still but that’s the minimal reproducible module with the issue:

This one doesn’t work

{ pkgs, lib, config, ... }:

let

  cfg = config.my.gui;

in {
  options = with lib; {
    my.gui = {
      enable = mkEnableOption ''
        Whether to enable gui module
      '';
    };
  };

  config = with lib;
    mkIf cfg.enable {
      users.users.foo = { packages = with pkgs; [ slack ]; }; # Slack will be installed but not linked
    };
}

This one works

{ pkgs, lib, config, ... }:

let

  cfg = config.my.gui;

in {
  options = with lib; {
    my.gui = {
      enable = mkEnableOption ''
        Whether to enable gui module
      '';
    };
  };

  config = with lib;
    mkIf cfg.enable {
      environment.systemPackages = with pkgs; [ slack ];  # Slack will be installed & linked inside /Applications
    };
}

I finally found my answer, in case it’s useful for anyone.

GUI apps only work with environment.systemPackages only in nix-darwin

1 Like