about summary refs log tree commit diff
path: root/modules/xdg/default.nix
blob: fc552883fb1645bb435a00ec7aabe2db479dc5f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{ lib, pkgs, config, ... }:

let
  inherit (lib) mapAttrsToList mkOption;
  inherit (lib.types) loaOf attrsOf path submodule;
  inherit (pkgs) linkFarm;

  userOptions = {
    options.xdg.config.paths = mkOption {
      default = {};
      type = attrsOf path;
    };
  };

in
  {
    options = {
      users.users = mkOption {
        type = loaOf (submodule userOptions);
      };
    };

    config = {
      environment.etc."xdg/nixos/per-user".source =
        linkFarm "xdg-config-users"
                  (mapAttrsToList (user: { xdg, ... }: {
                    name = user;
                    path = linkFarm "${user}-xdg-config-home"
                                    (mapAttrsToList
                                      (name: path: { inherit name path; })
                                      xdg.config.paths);
                  }) config.users.users);

      environment.extraInit = ''
        etc_xdg_config_home=/run/current-system/etc/xdg/nixos/per-user/$USER
        if [ -d "$etc_xdg_config_home" ]; then
            export XDG_CONFIG_HOME="''${XDG_CONFIG_HOME-$etc_xdg_config_home}"
        fi
        export XDG_DATA_HOME="''${XDG_DATA_HOME-$HOME/state}"
        export XDG_CACHE_HOME="''${XDG_DATA_HOME-$HOME/state/cache}"
      '';
    };
  }