Nix-darwin, home-manager, and flakes: how to set environment variables for the main user?

628 Views Asked by At

With nix-darwin, home-manager, using flakes, how to set environment variables for the main user ?

Here's a few approaches I've tried (copied from examples):
(this is a nix-darwin module)

{ pkgs, lib, config, options, ... }:
let
  user = config.users.primaryUser.name; # this value is set in another nix-darwin module

in
{
  home-manager.users."${user}" = {
    home.sessionVariables = {
      EXAMPLE_VAR = "6yuifhjbdsk";
    };
  };


  environment.variables = {
    EXAMPLE_VAR = "dhjakfhds";
  };

  # This seems to be nixOS-specific
  # environment.sessionVariables = {
  #   EXAMPLE_VAR = "798yfhudjksfh";
  # };
  environment.extraInit = with lib;
    concatStringsSep "\n"
      (mapAttrsToList (n: v: "export ${n}=\"${v}\"") config.environment.variables);
}
0

There are 0 best solutions below