zaphyra's git: nixfiles

zaphyra's nixfiles

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 
113 
114 
115 
116 
117 
118 
119 
120 
121 
{
  options,
  config,
  lib,
  pkgs,
  ...
}:
let
  inherit (lib) types;
  listOrSingletonOf =
    type: with types; coercedTo (either (listOf type) type) lib.toList (listOf type);

  cfg = config.xdg;
  mimeappsFormat = pkgs.formats.ini { listToValue = lib.concatStringsSep ";"; };

in
{

  options.xdg = {
    enable = lib.mkEnableOption "xdg foo";
    cacheDirectory = lib.mkOption {
      type = types.str;
      default = "~/.cache";
    };
    configDirectory = lib.mkOption {
      type = types.str;
      default = "~/.config";
    };

    dataDirectory = lib.mkOption {
      type = types.str;
      default = "~/.local/share";
    };

    stateDirectory = lib.mkOption {
      type = types.str;
      default = "~/.local/state";
    };

    mime-apps = {
      addedAssociations = lib.mkOption {
        type = with types; attrsOf (listOrSingletonOf str);
        default = { };
      };

      removedAssociations = lib.mkOption {
        type = with types; attrsOf (listOrSingletonOf str);
        default = { };
      };

      defaultApplications = lib.mkOption {
        type = with types; attrsOf (listOrSingletonOf str);
        default = { };
      };
    };
  };

  config = lib.mkIf cfg.enable {
    environment.sessionVariables = {
      XDG_CACHE_HOME = lib.mkIf (
        cfg.cacheDirectory != options.xdg.cacheDirectory.default
      ) cfg.cacheDirectory;
      XDG_CONFIG_HOME = lib.mkIf (
        cfg.configDirectory != options.xdg.configDirectory.default
      ) cfg.configDirectory;
      XDG_DATA_HOME = lib.mkIf (cfg.dataDirectory != options.xdg.dataDirectory.default) cfg.dataDirectory;
      XDG_STATE_HOME = lib.mkIf (
        cfg.stateDirectory != options.xdg.stateDirectory.default
      ) cfg.stateDirectory;
    };

    file = {
      xdg_config."mimeapps.list" =
        let
          nonDefault = {
            added = cfg.mime-apps.addedAssociations != options.xdg.mime-apps.addedAssociations.default;
            removed = cfg.mime-apps.removedAssociations != options.xdg.mime-apps.removedAssociations.default;
            default = cfg.mime-apps.defaultApplications != options.xdg.mime-apps.defaultApplications.default;
          };
        in
        lib.mkIf (lib.lists.any lib.trivial.id (lib.attrValues nonDefault)) {
          source = mimeappsFormat.generate "mimeapps.list" {
            "Added Associations" = cfg.mime-apps.addedAssociations;
            "Removed Associations" = cfg.mime-apps.removedAssociations;
            "Default Applications" = cfg.mime-apps.defaultApplications;
          };
        };
      xdg_data."glib-2.0/schemas".source = pkgs.buildEnv {
        name = "user-gsettings-schemas";
        paths = config.packages;
        pathsToLink = [ "/share/gsettings-schemas" ];
        ignoreCollisions = true;
        postBuild = ''
          if [ -x $out/bin/glib-compile-schemas -a -w $out/share/glib-2.0/schemas ]; then
              $out/bin/glib-compile-schemas $out/share/glib-2.0/schemas
          fi

          if [ -d  $out/share/gsettings-schemas/ ]; then
            # symlink any schema files to the standard schema directory
            for d in $out/share/gsettings-schemas/*; do
              # Force symlink, in case there are duplicates
              ln -fs $d/glib-2.0/schemas/*.xml $out
            done

            # and compile them
            if [ -w $out ]; then
              ${pkgs.glib.dev}/bin/glib-compile-schemas $out
            fi
          fi
        '';
      };

      # pkgs.symlinkJoin {
      #   name = "user-gsettings-schemas";
      #   paths = config.packages;
      #   stripPrefix = "/share/gsettings-schemas";
      # };
    };
  };

}