-
Notifications
You must be signed in to change notification settings - Fork 10
/
default.nix
138 lines (125 loc) · 5.12 KB
/
default.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
# This file is a toolbox file to parse a .nix directory and make
# 1. a nix overlay
# 2. a shell and a build derivation
with builtins;
let
toolboxDir = ./.;
get-path = src: f: let local = src + "/.nix/${f}"; in
if pathExists local then local else ./. + "/.nix/${f}";
in
{
src ? ./., # provide the current directory
config-file ? get-path src "config.nix",
fallback-file ? get-path src "fallback-config.nix",
nixpkgs-file ? get-path src "nixpkgs.nix",
shellHook-file ? get-path src "shellHook.sh",
overlays-dir ? get-path src "overlays",
coq-overlays-dir ? get-path src "coq-overlays",
ocaml-overlays-dir ? get-path src "ocaml-overlays",
ci-matrix ? false,
config ? {},
override ? {},
ocaml-override ? {},
global-override ? {},
withEmacs ? false,
print-env ? false,
do-nothing ? false,
update-nixpkgs ? false,
job ? null,
bundle ? null,
inNixShell ? null
}@args:
let
optionalImport = f: d:
if (isPath f || isString f) && pathExists f then import f else d;
do-nothing = (args.do-nothing or false) || update-nixpkgs || ci-matrix;
unNull = default: value: if isNull value then default else value;
initial = {
config = (optionalImport config-file (optionalImport fallback-file {}))
// config;
nixpkgs = optionalImport nixpkgs-file (throw "cannot find nixpkgs");
pkgs = import initial.nixpkgs {};
src = src;
lib = (initial.pkgs.coqPackages.lib or tmp-pkgs.lib)
// { diag = f: x: f x x; };
inherit overlays-dir coq-overlays-dir ocaml-overlays-dir;
inherit global-override override ocaml-override;
};
my-throw = x: throw "Coq nix toolbox error: ${x}";
in
with initial.lib; let
inNixShell = args.inNixShell or trivial.inNixShell;
setup = switch initial.config.format [
{ case = "1.0.0"; out = import ./config-parser-1.0.0 initial; }
{ case = x: !isString x; out = my-throw "config.format must be a string."; }
] (my-throw "config.format ${initial.config.format} not supported");
instances = setup.instances;
selectedBundle = let dflt = setup.config.default-bundle; in
if isNull bundle || bundle == "_all" then dflt else bundle;
allBundles = bundle == "_all";
selected-instance = instances."${selectedBundle}";
shellHook = readFile shellHook-file
+ optionalString print-env "\nprintNixEnv; exit"
+ optionalString update-nixpkgs "\nupdateNixpkgsUnstable; exit"
+ optionalString ci-matrix "\nnixBundles; exit";
jsonBundles = toJSON (attrNames setup.bundles);
jsonBundleSet = toJSON setup.bundles;
jsonBundle = toJSON selected-instance.bundle;
coq-lsp = if selected-instance.pkgs.coqPackages?coq-lsp then
[ selected-instance.pkgs.coqPackages.coq-lsp ] else [];
vscoq = if selected-instance.pkgs.coqPackages?vscoq-language-server then
[ selected-instance.pkgs.coqPackages.vscoq-language-server ] else [];
emacs = with selected-instance.pkgs; emacsWithPackages
(epkgs: with epkgs.melpaPackages; [ proof-general ]);
emacsInit = ./emacs-init.el;
jsonSetupConfig = toJSON setup.config;
ciByBundle = flip mapAttrs setup.instances (_: v:
mapAttrs (_: x: map (x: x.name) x) v.ci.set);
jsonCIbyBundle = toJSON ciByBundle;
ciByJob =
let
jobs-list = attrValues (flip mapAttrs ciByBundle (tn: tv:
flip mapAttrs tv (jn: jv: {${tn} = jv;})));
push-list = foldAttrs (n: a: [n] ++ a) [];
in
flip mapAttrs (push-list jobs-list)
(jn: jv: mapAttrs (_: flatten) (push-list jv));
jsonCIbyJob = toJSON ciByJob;
mkDeriv = shell:
if !inNixShell then shell
else with selected-instance; shell.overrideAttrs (old: {
inherit (setup.config) nixpkgs coqproject;
inherit jsonBundle jsonBundles jsonSetupConfig jsonCIbyBundle jsonBundleSet
jsonCIbyJob shellHook toolboxDir selectedBundle
jsonPkgsDeps jsonPkgsRevDeps jsonPkgsSorted jsonActionFile;
bundles = attrNames setup.bundles;
passthru = (old.passthru or {}) // {inherit action pkgs;};
COQBIN = optionalString (!do-nothing) "";
coq_version = optionalString (!do-nothing)
pkgs.coqPackages.coq.coq-version;
nativeBuildInputs = optionals (!do-nothing)
((old.nativeBuildInputs or []) ++ coq-lsp ++ vscoq) ++ [ pkgs.remarshal ];
propagatedNativeBuildInputs = optionals (!do-nothing)
(old.propagatedNativeBuildInputs or []);
buildInputs = optionals (!do-nothing) (old.buildInputs or []);
propagatedBuildInputs = optionals (!do-nothing)
(old.propagatedBuildInputs or []);
}
// optionalAttrs withEmacs {
inherit emacsInit;
emacsBin = "${emacs}" + "/bin/emacs";
});
nix-ci = job: map mkDeriv (if allBundles
then flatten (mapAttrsToList (_: i: i.ci.subpkgs job) instances)
else instances.${selectedBundle}.ci.subpkgs job);
nix-default = if allBundles
then mapAttrsToList (_: i: mkDeriv i.this-shell-pkg) instances
else mkDeriv selected-instance.this-shell-pkg;
nix-auto = if isNull job then nix-default else nix-ci job;
in
if !isDerivation nix-auto then nix-auto
else nix-auto.overrideAttrs (o: {
passthru = (o.passthru or {})
// { inherit initial setup shellHook;
inherit nix-default nix-ci nix-auto; };
})