-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeode.shard
165 lines (129 loc) · 5.78 KB
/
geode.shard
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
rec {
lib = rec {
mkPackage = pkgSet: pkgSet // { __functor = self: config: builtins.mergeTree self { inherit config; }; };
};
util = rec {
ensureExists = { path, default ? null }: assert geode.file.exists path; default;
dirEnts = dir: let
found = shell.find [
dir
"("
"-type" "f"
"-o" "-type" "d"
")"
"-printf"
"%P\n"
];
in assert found.exitCode == 0; builtins.split "\n" found.stdout;
mkdirIfNotExists = path: if geode.file.exists path
then 0
else assert (geode.file.mkdir path) == 0; path;
mkdirRec = path: if geode.file.exists path
then 0
else builtins.seq
(mkdirRec (geode.file.dirname path))
(geode.file.mkdir path);
merge = fst: snd: fst // snd;
installFile = { src, dst, mode ? "744" }: let dstDir = geode.file.dirname (geode.prefix + dst);
in assert (mkdirRec dstDir) == 0;
shell.install ["-m" mode src (geode.prefix + dst)];
installFiles = builtins.map installFile;
writeFile = file: data: builtins.seq
(mkdirRec $ geode.file.dirname file)
(geode.file.writeFile file $ builtins.toString data);
# utility functions that mimic shell commands
shell = {
cp = args:
geode.proc.spawn "cp" (builtins.map builtins.toString args) true;
make = args:
geode.proc.spawn "make" (builtins.map builtins.toString (args ++ ["-j" (geode.proc.nJobs)])) true;
find = args:
geode.proc.spawnPipe "find" (builtins.map builtins.toString args) "";
install = args:
geode.proc.spawn "install" (builtins.map builtins.toString args) true;
xorriso = args:
geode.proc.spawn "xorriso" (builtins.map builtins.toString args) true;
nproc = geode.proc.spawn "nproc" [] true;
};
paths = rec {
prefix = ensureExists { path = geode.prefix; default = geode.prefix; };
store = ensureExists { path = geode.store; default = geode.store; };
gcc_specs = let path = prefix + "/lib/gcc/cross-libc.specs";
in ensureExists { inherit path; default = path; };
};
archive = {
mkisofs = { input, output ? "amethyst.iso", uefi ? null }: geode.proc.spawn "xorriso" [
"-as" "mkisofs"
input
"-o" output
] true;
};
io = rec {
term = rec {
reset = "\e[0m";
bold = "\e[1m";
red = "\e[31m";
green = "\e[32m";
yellow = "\e[33m";
blue = "\e[34m";
magenta = "\e[35m";
cyan = "\e[36m";
color = c: s: c + s + reset;
};
fmtMajor = str: term.bold + (term.color term.cyan str);
logMajor = str: task: builtins.seq (geode.debug.println $ fmtMajor str) task;
};
};
templates = {
staticConfig = {
boot.kernelParams = [];
boot.initrd = {
compression = "ustar";
path = /boot/initrd;
enabled = false;
};
environment.systemPackages = [];
};
};
manager = rec {
pkgAttrs = pkg: builtins.head (builtins.attrValues pkg);
pkgOpts = pkg: (pkgAttrs pkg).opts or {};
pkgName = pkg: (pkgAttrs pkg).name or null;
pkgBuildDependencies = pkg: pkg.buildDependencies or [];
announceBuild = pkg: util.io.logMajor ("Building " + pkg.name + "...");
announceInstall = pkg: util.io.logMajor ("Installing " + pkg.name + "...");
newTask = name: task: if task == null then [] else
[{ inherit name task; }];
buildPipeline = config: pkg: builtins.foldl (a: b: a ++ b) [] (builtins.map (installPipeline config) $ pkgBuildDependencies pkg)
++ (announceBuild pkg (
(newTask "fetch" pkg.fetch or null)
++ (newTask "unpack" pkg.unpack or null)
++ (newTask "patch" pkg.patch or null)
++ (newTask "configure" pkg.configure or null)
++ (newTask "build" pkg.build or null)
++ (newTask "check" pkg.check or null)
));
installPipeline = config: pkg: let
noEnableFlag = c: true;
in if (pkg.isEnabled or noEnableFlag) config
then (buildPipeline config pkg) ++ (announceInstall pkg $ newTask "install" pkg.install or null)
else [];
installPkg = pkg: with builtins;
let
taskError = msg: name: geode.error.throw $ "Task '" + name + "' on package '" + pkg.name + "' " + msg + ".";
successful = name: task:
if isNull task then true
else if isInt task then task == 0
else if isList task then all (successful name) task
else taskError "returned unexpected value" name;
reportError = { name, task }: if successful name task
then true
else taskError ("returned non-zero exit code: " + $ toString task) name;
in all reportError $ manager.installPipeline config pkg;
};
pkgIndex = let
found = util.shell.find [(geode.store + "/pkgs") "-type" "f" "-name" "*.shard"];
in assert found.exitCode == 0; builtins.split "\n" found.stdout;
pkgList = builtins.map import pkgIndex;
pkgs = builtins.foldl util.merge {} pkgList;
}