UNPKG

1.87 kBPlain TextView Raw
1import Doer from './Doer'
2
3const VERSION = require('../package').version
4
5/**
6 * Generates a default.nix for a `SoftwareEnvironment` instance
7 */
8export default class NixGenerator extends Doer {
9
10 /**
11 * Generate a default.nix file for a `SoftwareEnvironment` instance
12 *
13 * @param environ `SoftwareEnvironment` instance
14 */
15 generate (environ: any, folder: string): string {
16 this.folder = folder
17
18 let comments = true
19
20 let nixfile = ''
21
22 if (!environ) return ''
23
24 if (comments) {
25 nixfile += `# Generated by Dockter ${VERSION} at ${new Date().toISOString()}
26# To stop Dockter generating this file and start editing it yourself,
27# rename it to "default.nix".\n`
28 }
29
30 nixfile += `with import (builtins.fetchTarball {
31 name = "stencila-18.12";
32 url = https://github.com/stencila/nixpkgs/archive/18.12.tar.gz;
33 sha256 = "0kygd44qc2d41dh6pccjiisdvxgpnj9njmhalr0mhrh971xxgnkz";
34}) {};
35
36stdenv.mkDerivation rec {
37 name = "${environ.name}";
38 buildInputs = [
39 bashInteractive coreutils utillinux findutils gnugrep which openssl cacert
40`
41
42 for (let softwareRequirement of environ.softwareRequirements) {
43
44 let platform = softwareRequirement.runtimePlatform
45 if (platform === 'R') { nixfile += ` R\n` }
46 if (platform === 'Node.js') { nixfile += ` nodejs\n` }
47 if (platform === 'Python') { nixfile += ` python37\n` }
48
49 let language = platform.toLowerCase().replace(/\.[^/.]+$/, '')
50 if (language === 'python') { language = 'python37' }
51
52 let pkgs = softwareRequirement.softwareRequirements.map(
53 (x: any) => `${language}Packages.${x.name.toLowerCase()}`
54 ).join(' ')
55
56 nixfile += ` ${pkgs}\n`
57 }
58
59 nixfile = nixfile.trim()
60 nixfile += `
61 ];
62}\n`
63
64 // Write `.default.nix` for use by Nix
65 this.write('.default.nix', nixfile)
66
67 return nixfile
68 }
69}