fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
3.6 KiB129 linesraw
1{ options, config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.xdg;
8
9  fileType = (import ../lib/file-type.nix {
10    inherit (config.home) homeDirectory;
11    inherit lib pkgs;
12  }).fileType;
13
14  defaultCacheHome = "${config.home.homeDirectory}/.cache";
15  defaultConfigHome = "${config.home.homeDirectory}/.config";
16  defaultDataHome = "${config.home.homeDirectory}/.local/share";
17  defaultStateHome = "${config.home.homeDirectory}/.local/state";
18
19  getEnvFallback = name: fallback:
20    let value = builtins.getEnv name;
21    in if value != "" then value else fallback;
22
23in {
24  options.xdg = {
25    enable = mkEnableOption "management of XDG base directories";
26
27    cacheHome = mkOption {
28      type = types.path;
29      defaultText = "~/.cache";
30      apply = toString;
31      description = ''
32        Absolute path to directory holding application caches.
33      '';
34    };
35
36    configFile = mkOption {
37      type = fileType "xdg.configFile" "{var}`xdg.configHome`" cfg.configHome;
38      default = { };
39      description = ''
40        Attribute set of files to link into the user's XDG
41        configuration home.
42      '';
43    };
44
45    configHome = mkOption {
46      type = types.path;
47      defaultText = "~/.config";
48      apply = toString;
49      description = ''
50        Absolute path to directory holding application configurations.
51      '';
52    };
53
54    dataFile = mkOption {
55      type =
56        fileType "xdg.dataFile" "<varname>xdg.dataHome</varname>" cfg.dataHome;
57      default = { };
58      description = ''
59        Attribute set of files to link into the user's XDG
60        data home.
61      '';
62    };
63
64    dataHome = mkOption {
65      type = types.path;
66      defaultText = "~/.local/share";
67      apply = toString;
68      description = ''
69        Absolute path to directory holding application data.
70      '';
71    };
72
73    stateHome = mkOption {
74      type = types.path;
75      defaultText = "~/.local/state";
76      apply = toString;
77      description = ''
78        Absolute path to directory holding application states.
79      '';
80    };
81  };
82
83  config = mkMerge [
84    (let
85      variables = {
86        XDG_CACHE_HOME = cfg.cacheHome;
87        XDG_CONFIG_HOME = cfg.configHome;
88        XDG_DATA_HOME = cfg.dataHome;
89        XDG_STATE_HOME = cfg.stateHome;
90      };
91    in mkIf cfg.enable {
92      xdg.cacheHome = mkDefault defaultCacheHome;
93      xdg.configHome = mkDefault defaultConfigHome;
94      xdg.dataHome = mkDefault defaultDataHome;
95      xdg.stateHome = mkDefault defaultStateHome;
96
97      home.sessionVariables = variables;
98      systemd.user.sessionVariables =
99        mkIf pkgs.stdenv.hostPlatform.isLinux variables;
100    })
101
102    # Legacy non-deterministic setup.
103    (mkIf (!cfg.enable && versionOlder config.home.stateVersion "20.09") {
104      xdg.cacheHome =
105        mkDefault (getEnvFallback "XDG_CACHE_HOME" defaultCacheHome);
106      xdg.configHome =
107        mkDefault (getEnvFallback "XDG_CONFIG_HOME" defaultConfigHome);
108      xdg.dataHome = mkDefault (getEnvFallback "XDG_DATA_HOME" defaultDataHome);
109    })
110
111    # "Modern" deterministic setup.
112    (mkIf (!cfg.enable && versionAtLeast config.home.stateVersion "20.09") {
113      xdg.cacheHome = mkDefault defaultCacheHome;
114      xdg.configHome = mkDefault defaultConfigHome;
115      xdg.dataHome = mkDefault defaultDataHome;
116      xdg.stateHome = mkDefault defaultStateHome;
117    })
118
119    {
120      home.file = mkMerge [
121        (mapAttrs' (name: file: nameValuePair "${cfg.configHome}/${name}" file)
122          cfg.configFile)
123        (mapAttrs' (name: file: nameValuePair "${cfg.dataHome}/${name}" file)
124          cfg.dataFile)
125        { "${cfg.cacheHome}/.keep".text = ""; }
126      ];
127    }
128  ];
129}