-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
186 lines (161 loc) · 4.94 KB
/
flake.nix
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
{
inputs.treefmt-nix.url = "github:numtide/treefmt-nix";
outputs =
{
nixpkgs,
treefmt-nix,
self,
...
}:
let
inherit (nixpkgs) lib;
perSystem =
f:
lib.genAttrs lib.systems.flakeExposed (
system:
f {
inherit system;
pkgs = nixpkgs.legacyPackages.${system};
}
);
treefmt =
pkgs:
treefmt-nix.lib.evalModule pkgs {
projectRootFile = "flake.nix";
programs.nixfmt.enable = true;
programs.nixfmt.excludes = [ ".direnv" ];
programs.deadnix.enable = true;
};
formatter = perSystem ({ pkgs, ... }: (treefmt pkgs).config.build.wrapper);
libs = pkgs: rec {
direnv_bash = pkgs.substitute {
src = ./direnv.bash;
substitutions = [
"--subst-var-by"
"MAIN_FLAKE"
./.
];
};
install = pkgs.writeShellScriptBin "install" ''
mkdir -p $HOME/.config/direnv/lib
ln -sfn ${direnv_bash} $HOME/.config/direnv/lib/use_devshell_toml.sh
echo "Installed use_devshell_toml.sh on direnv lib."
'';
default = pkgs.writeShellApplication {
name = "use_devshell_toml";
text = ''
if ! test -e "$HOME/.config/direnv/lib/use_devshell_toml.sh"; then
${install}/bin/install
fi
test -z "''${1:-}" && exit 0 # terminate if no package names were given
for package in "''${@:-}"; do
echo "Adding package '$package' to devshell.toml"
printf '\n[[commands]]\npackage = "%s"\n' "$package" >> devshell.toml
done
if ! test -e "$PWD/.envrc"; then
echo "use devshell_toml" > "$PWD/.envrc"
fi
direnv allow
'';
};
demo = pkgs.writeShellApplication {
name = "demo";
text = ''
set -a
export BASE="$PWD"
export PATH="${
with pkgs;
lib.makeBinPath [
vhs
nix
direnv
coreutils
which
bash
]
}"
HOME="$(mktemp -d)"
export HOME
cd "$HOME"
mkdir -p "$HOME/.config/nix"
echo "extra-experimental-features = nix-command flakes" > "$HOME/.config/nix/nix.conf"
direnv hook bash > .hook
# shellcheck source=/dev/null
source .hook
vhs "$BASE/demo.tape"
cp -f "$PWD/demo.gif" "$BASE/demo.gif"
'';
};
gen-flakes = pkgs.writeShellApplication {
name = "gen-flakes";
runtimeInputs = with pkgs; [
coreutils
gnused
nix
];
text = ''
SOURCE_DIR="$1"
FLAKE_DEST="$2"
export NIX_CONFIG="extra-experimental-features = flakes nix-command"
mkdir -p "$FLAKE_DEST/config"
sed -e "s#config.url = \"path:\"#config.url = \"path:$FLAKE_DEST/config\"#;" ${./lib/devshell-flake.nix} > "$FLAKE_DEST/flake.nix"
cp -f "$SOURCE_DIR/devshell.toml" "$FLAKE_DEST/devshell.toml"
if test -e "$SOURCE_DIR/flake.toml"; then
nix eval \
--file ${./lib/make-config-flake.nix} \
--apply "f: f $SOURCE_DIR/flake.toml" \
--raw --impure --offline |\
sed -e "s#url = \"path:./#url = \"path:$SOURCE_DIR/#g" > "$FLAKE_DEST/config/flake.nix"
else
cp -f ${./lib/empty-config-flake.nix} "$FLAKE_DEST/config/flake.nix"
fi
'';
};
test-templates = pkgs.writeShellApplication {
name = "test-templates";
text = ''
env -i IS_DARWIN="${builtins.toString pkgs.stdenv.isDarwin}" PATH="${
with pkgs;
lib.makeBinPath [
direnv
nix
coreutils
jq
bash
]
}" ${pkgs.bash}/bin/bash --noprofile --norc ${./test-templates.bash} "''${@}"
'';
};
};
packages = perSystem (
{ pkgs, ... }:
{
inherit (libs pkgs)
default
demo
install
gen-flakes
test-templates
;
}
);
checks = perSystem (
{ pkgs, ... }:
{
formatting = (treefmt pkgs).config.build.check self;
}
);
in
{
inherit formatter packages checks;
templates = {
default.path = ./templates/default;
default.description = "Simple toml devshell";
};
homeModules.default =
{ pkgs, ... }:
{
home.file.".config/direnv/lib/use_devshell_toml.sh".source = (libs pkgs).direnv_bash;
};
};
}