1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const fs_1 = __importDefault(require("fs"));
|
7 | const path_1 = __importDefault(require("path"));
|
8 | const child_process_1 = require("child_process");
|
9 | const dockerode_1 = __importDefault(require("dockerode"));
|
10 | const js_yaml_1 = __importDefault(require("js-yaml"));
|
11 | const NixGenerator_1 = __importDefault(require("./NixGenerator"));
|
12 | const CachingUrlFetcher_1 = __importDefault(require("./CachingUrlFetcher"));
|
13 | const generator = new NixGenerator_1.default(new CachingUrlFetcher_1.default(), undefined);
|
14 | const docker = new dockerode_1.default();
|
15 |
|
16 |
|
17 |
|
18 | function compile(environ, folder) {
|
19 |
|
20 | generator.generate(environ, folder);
|
21 |
|
22 | let defaultNix = path_1.default.join(folder, 'default.nix');
|
23 | let dockterNix = path_1.default.join(folder, '.default.nix');
|
24 | let nixfile = fs_1.default.existsSync(defaultNix) ? defaultNix : dockterNix;
|
25 |
|
26 | let dockerfile = path_1.default.join(folder, '.nixDockerfile');
|
27 | fs_1.default.writeFileSync(dockerfile, `FROM nixos/nix
|
28 |
|
29 | # Copy over the Nix derivation
|
30 | COPY ${path_1.default.basename(nixfile)} default.nix
|
31 | # Run nix-shell
|
32 | CMD nix-shell --pure\n`);
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 | async function build(folder) {
|
38 | let name = path_1.default.basename(folder).toLocaleLowerCase().replace(' ', '-');
|
39 |
|
40 | let defaultNix = path_1.default.join(folder, 'default.nix');
|
41 | let dockterNix = path_1.default.join(folder, '.default.nix');
|
42 | let nixfile = fs_1.default.existsSync(defaultNix) ? defaultNix : dockterNix;
|
43 |
|
44 | let build = await docker.buildImage({
|
45 | context: folder,
|
46 | src: ['.nixDockerfile', path_1.default.basename(nixfile)]
|
47 | }, { t: name, dockerfile: '.nixDockerfile' });
|
48 |
|
49 | docker.modem.followProgress(build, (err, res) => {
|
50 | if (err)
|
51 | throw err;
|
52 | output(res);
|
53 | });
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 | async function execute(folder, command = '') {
|
59 |
|
60 | let volumes = await docker.listVolumes();
|
61 | let nixStoreVolume = volumes.Volumes.find((vol) => (vol.Name === 'nix-store'));
|
62 | if (!nixStoreVolume) {
|
63 | await docker.createVolume({ name: 'nix-store' });
|
64 | }
|
65 | let name = path_1.default.basename(folder).toLocaleLowerCase().replace(' ', '-');
|
66 | let args = `run -it --rm -v /tmp:/tmp -v nix-store:/nix ${name}`;
|
67 |
|
68 |
|
69 | if (command)
|
70 | args += ` /root/.nix-profile/bin/nix-shell --pure --run "${command.replace('"', '\\"')}"`;
|
71 | child_process_1.spawnSync('docker', args.split(' '), {
|
72 | shell: true,
|
73 | cwd: process.cwd(),
|
74 | stdio: 'inherit'
|
75 | });
|
76 | }
|
77 | exports.default = { compile, build, execute };
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | function output(object, format = 'json') {
|
85 | if (object)
|
86 | console.log(format === 'yaml' ? js_yaml_1.default.safeDump(object, { lineWidth: 120 }) : JSON.stringify(object, null, ' '));
|
87 | }
|