1{ config, lib, pkgs, ... }:
2
3let
4
5 cfg = config.programs.zsh.ohMyZsh;
6
7 mkLinkFarmEntry = name: dir:
8 let
9 env = pkgs.buildEnv {
10 name = "zsh-${name}-env";
11 paths = cfg.customPkgs;
12 pathsToLink = "/share/zsh/${dir}";
13 };
14 in
15 { inherit name; path = "${env}/share/zsh/${dir}"; };
16
17 mkLinkFarmEntry' = name: mkLinkFarmEntry name name;
18
19 custom =
20 if cfg.custom != null then cfg.custom
21 else if builtins.length cfg.customPkgs == 0 then null
22 else pkgs.linkFarm "oh-my-zsh-custom" [
23 (mkLinkFarmEntry' "themes")
24 (mkLinkFarmEntry "completions" "site-functions")
25 (mkLinkFarmEntry' "plugins")
26 ];
27
28in
29 {
30 imports = [
31 (lib.mkRenamedOptionModule [ "programs" "zsh" "oh-my-zsh" "enable" ] [ "programs" "zsh" "ohMyZsh" "enable" ])
32 (lib.mkRenamedOptionModule [ "programs" "zsh" "oh-my-zsh" "theme" ] [ "programs" "zsh" "ohMyZsh" "theme" ])
33 (lib.mkRenamedOptionModule [ "programs" "zsh" "oh-my-zsh" "custom" ] [ "programs" "zsh" "ohMyZsh" "custom" ])
34 (lib.mkRenamedOptionModule [ "programs" "zsh" "oh-my-zsh" "plugins" ] [ "programs" "zsh" "ohMyZsh" "plugins" ])
35 ];
36
37 options = {
38 programs.zsh.ohMyZsh = {
39 enable = lib.mkOption {
40 type = lib.types.bool;
41 default = false;
42 description = ''
43 Enable oh-my-zsh.
44 '';
45 };
46
47 package = lib.mkPackageOption pkgs "oh-my-zsh" { };
48
49 plugins = lib.mkOption {
50 default = [];
51 type = lib.types.listOf(lib.types.str);
52 description = ''
53 List of oh-my-zsh plugins
54 '';
55 };
56
57 custom = lib.mkOption {
58 default = null;
59 type = with lib.types; nullOr str;
60 description = ''
61 Path to a custom oh-my-zsh package to override config of oh-my-zsh.
62 (Can't be used along with `customPkgs`).
63 '';
64 };
65
66 customPkgs = lib.mkOption {
67 default = [];
68 type = lib.types.listOf lib.types.package;
69 description = ''
70 List of custom packages that should be loaded into `oh-my-zsh`.
71 '';
72 };
73
74 theme = lib.mkOption {
75 default = "";
76 type = lib.types.str;
77 description = ''
78 Name of the theme to be used by oh-my-zsh.
79 '';
80 };
81
82 cacheDir = lib.mkOption {
83 default = "$HOME/.cache/oh-my-zsh";
84 type = lib.types.str;
85 description = ''
86 Cache directory to be used by `oh-my-zsh`.
87 Without this option it would default to the read-only nix store.
88 '';
89 };
90 };
91 };
92
93 config = lib.mkIf cfg.enable {
94
95 # Prevent zsh from overwriting oh-my-zsh's prompt
96 programs.zsh.promptInit = lib.mkDefault "";
97
98 environment.systemPackages = [ cfg.package ];
99
100 programs.zsh.interactiveShellInit = ''
101 # oh-my-zsh configuration generated by NixOS
102 export ZSH=${cfg.package}/share/oh-my-zsh
103
104 ${lib.optionalString (builtins.length(cfg.plugins) > 0)
105 "plugins=(${builtins.concatStringsSep " " cfg.plugins})"
106 }
107
108 ${lib.optionalString (custom != null)
109 "ZSH_CUSTOM=\"${custom}\""
110 }
111
112 ${lib.optionalString (builtins.stringLength(cfg.theme) > 0)
113 "ZSH_THEME=\"${cfg.theme}\""
114 }
115
116 ${lib.optionalString (cfg.cacheDir != null) ''
117 if [[ ! -d "${cfg.cacheDir}" ]]; then
118 mkdir -p "${cfg.cacheDir}"
119 fi
120 ZSH_CACHE_DIR=${cfg.cacheDir}
121 ''}
122
123 source $ZSH/oh-my-zsh.sh
124 '';
125
126 assertions = [
127 {
128 assertion = cfg.custom != null -> cfg.customPkgs == [];
129 message = "If `cfg.custom` is set for `ZSH_CUSTOM`, `customPkgs` can't be used!";
130 }
131 ];
132
133 };
134
135 meta.doc = ./oh-my-zsh.md;
136 }