-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhomebrew.nix
More file actions
141 lines (127 loc) · 4.25 KB
/
Copy pathhomebrew.nix
File metadata and controls
141 lines (127 loc) · 4.25 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
{
config,
inputs ? { },
lib,
pkgs,
primaryUser ? null,
...
}:
let
inherit (lib)
mkEnableOption
mkIf
mkOption
optionalAttrs
types
;
# nix-homebrew currently hardcodes Ruby 3.4, but the repo-pinned Homebrew
# source requires Ruby 4.0. Keep the patch local while advancing the tested
# brew/tap tuple explicitly.
brewSource =
if
builtins.hasAttr "nix-homebrew" inputs
&& builtins.hasAttr "inputs" inputs."nix-homebrew"
&& builtins.hasAttr "brew-src" inputs."nix-homebrew".inputs
then
inputs."nix-homebrew".inputs.brew-src
else
null;
brewVersion =
if brewSource != null && brewSource ? rev then
"HEAD-${builtins.substring 0 7 brewSource.rev}"
else
"HEAD";
patchedBrewPackage =
if brewSource == null then
null
else
pkgs.runCommandLocal "brew-${brewVersion}-nixcfg-patched" { } ''
cp -r "${brewSource}" "$out"
chmod u+w "$out" "$out/Library/Homebrew" "$out/Library/Homebrew/cmd"
substituteInPlace "$out/Library/Homebrew/cmd/update.sh" \
--replace-fail 'for DIR in "''${HOMEBREW_REPOSITORY}"' "for DIR in "
ruby_sh="$out/Library/Homebrew/utils/ruby.sh"
if [[ ! -e "$ruby_sh" ]]; then
printf 'Expected Homebrew ruby helper at %s\n' "$ruby_sh" >&2
exit 1
fi
if ! grep -q 'setup-ruby-path' "$ruby_sh"; then
printf 'Expected setup-ruby-path in %s\n' "$ruby_sh" >&2
exit 1
fi
chmod u+w "$ruby_sh"
cat >>"$ruby_sh" <<'EOF'
# nixcfg: use the Nix-provided Ruby while preserving Homebrew's setup-ruby-path
# bookkeeping (GEM_HOME, BUNDLE_GEMFILE, bootsnap path, etc.). Nix's Ruby 4.0
# packages fiddle as a default gem, so Homebrew's default --disable=gems would
# hide it from linkage_checker.rb.
export HOMEBREW_RUBY_DISABLE_OPTIONS="--disable=rubyopt"
system_ruby_supported() { return 0; }
find_ruby() { echo "${pkgs.ruby_4_0}/bin/ruby"; }
EOF
brew_sh="$out/Library/Homebrew/brew.sh"
chmod u+w "$brew_sh"
sed -i -e 's/^HOMEBREW_VERSION=.*/HOMEBREW_VERSION="${brewVersion}"/g' "$brew_sh"
sed -i -e 's/^GIT_REVISION=.*/GIT_REVISION=""; HOMEBREW_VERSION="${brewVersion}"/g' "$brew_sh"
'';
defaultTaps =
(optionalAttrs (builtins.hasAttr "homebrew-core" inputs) {
"homebrew/homebrew-core" = inputs."homebrew-core";
})
// (optionalAttrs (builtins.hasAttr "homebrew-cask" inputs) {
"homebrew/homebrew-cask" = inputs."homebrew-cask";
})
// (optionalAttrs (builtins.hasAttr "pantsbuild-tap" inputs) {
"pantsbuild/homebrew-tap" = inputs."pantsbuild-tap";
});
cfg = config.nixcfg.darwin.homebrew;
resolvedUser = if cfg.user != null then cfg.user else primaryUser;
in
{
options.nixcfg.darwin.homebrew = {
enable = mkEnableOption "managed nix-homebrew configuration" // {
default = true;
};
user = mkOption {
type = types.nullOr types.str;
default = primaryUser;
description = "User that owns the Homebrew installation.";
};
enableRosetta = mkOption {
type = types.bool;
default = true;
description = "Enable Rosetta support in nix-homebrew on Apple Silicon.";
};
taps = mkOption {
type = types.attrsOf types.anything;
default = defaultTaps;
description = "Tap attrset passed directly to nix-homebrew.taps.";
};
mutableTaps = mkOption {
type = types.bool;
default = false;
description = "Whether Homebrew taps may be modified outside nix-homebrew.";
};
};
config = mkIf cfg.enable {
assertions = [
{
assertion = resolvedUser != null;
message = "nixcfg.darwin.homebrew.user must be set (or provide primaryUser via lib.mkSystem/lib.mkDarwinHost).";
}
];
nix-homebrew = {
enable = true;
inherit (cfg)
enableRosetta
mutableTaps
taps
;
user = resolvedUser;
}
// optionalAttrs (patchedBrewPackage != null) {
package = patchedBrewPackage;
patchBrew = false;
};
};
}