aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/misc/xdg.nix
blob: 7420e8e92b3c99557d72035da928f402475b9f58 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{ options, config, lib, pkgs, ... }:

with lib;

let

  cfg = config.xdg;

  dag = config.lib.dag;

  fileType = (import ../lib/file-type.nix {
    inherit (config.home) homeDirectory;
    inherit lib pkgs;
  }).fileType;

  defaultCacheHome = "${config.home.homeDirectory}/.cache";
  defaultConfigHome = "${config.home.homeDirectory}/.config";
  defaultDataHome = "${config.home.homeDirectory}/.local/share";

  getXdgDir = name: fallback:
    let
      value = builtins.getEnv name;
    in
      if value != "" then value else fallback;

in

{
  options.xdg = {
    enable = mkEnableOption "management of XDG base directories";

    cacheHome = mkOption {
      type = types.path;
      defaultText = "~/.cache";
      description = ''
        Absolute path to directory holding application caches.
      '';
    };

    configFile = mkOption {
      type = fileType "<varname>xdg.configHome</varname>" cfg.configHome;
      default = {};
      description = ''
        Attribute set of files to link into the user's XDG
        configuration home.
      '';
    };

    configHome = mkOption {
      type = types.path;
      defaultText = "~/.config";
      description = ''
        Absolute path to directory holding application configurations.
      '';
    };

    dataFile = mkOption {
      type = fileType "<varname>xdg.dataHome</varname>" cfg.dataHome;
      default = {};
      description = ''
        Attribute set of files to link into the user's XDG
        data home.
      '';
    };

    dataHome = mkOption {
      type = types.path;
      defaultText = "~/.local/share";
      description = ''
        Absolute path to directory holding application data.
      '';
    };
  };

  config = mkMerge [
    (mkIf cfg.enable {
      xdg.cacheHome = mkDefault defaultCacheHome;
      xdg.configHome = mkDefault defaultConfigHome;
      xdg.dataHome = mkDefault defaultDataHome;

      home.sessionVariables = {
        XDG_CACHE_HOME = cfg.cacheHome;
        XDG_CONFIG_HOME = cfg.configHome;
        XDG_DATA_HOME = cfg.dataHome;
      };
    })

    # Legacy non-deterministic setup.
    (mkIf (!cfg.enable && versionOlder config.home.stateVersion "20.09") {
      xdg.cacheHome = getXdgDir "XDG_CACHE_HOME" defaultCacheHome;
      xdg.configHome = getXdgDir "XDG_CONFIG_HOME" defaultConfigHome;
      xdg.dataHome = getXdgDir "XDG_DATA_HOME" defaultDataHome;
    })

    # "Modern" deterministic setup.
    (mkIf (!cfg.enable && versionAtLeast config.home.stateVersion "20.09") {
      xdg.cacheHome = mkDefault defaultCacheHome;
      xdg.configHome = mkDefault defaultConfigHome;
      xdg.dataHome = mkDefault defaultDataHome;
    })

    {
      home.file = mkMerge [
        cfg.configFile
        cfg.dataFile
        {
          "${config.xdg.cacheHome}/.keep".text = "";
        }
      ];
    }
  ];
}