import Doer from './Doer' const VERSION = require('../package').version /** * Generates a default.nix for a `SoftwareEnvironment` instance */ export default class NixGenerator extends Doer { /** * Generate a default.nix file for a `SoftwareEnvironment` instance * * @param environ `SoftwareEnvironment` instance */ generate (environ: any, folder: string): string { this.folder = folder let comments = true let nixfile = '' if (!environ) return '' if (comments) { nixfile += `# Generated by Dockter ${VERSION} at ${new Date().toISOString()} # To stop Dockter generating this file and start editing it yourself, # rename it to "default.nix".\n` } nixfile += `with import (builtins.fetchTarball { name = "stencila-18.12"; url = https://github.com/stencila/nixpkgs/archive/18.12.tar.gz; sha256 = "0kygd44qc2d41dh6pccjiisdvxgpnj9njmhalr0mhrh971xxgnkz"; }) {}; stdenv.mkDerivation rec { name = "${environ.name}"; buildInputs = [ bashInteractive coreutils utillinux findutils gnugrep which openssl cacert ` for (let softwareRequirement of environ.softwareRequirements) { let platform = softwareRequirement.runtimePlatform if (platform === 'R') { nixfile += ` R\n` } if (platform === 'Node.js') { nixfile += ` nodejs\n` } if (platform === 'Python') { nixfile += ` python37\n` } let language = platform.toLowerCase().replace(/\.[^/.]+$/, '') if (language === 'python') { language = 'python37' } let pkgs = softwareRequirement.softwareRequirements.map( (x: any) => `${language}Packages.${x.name.toLowerCase()}` ).join(' ') nixfile += ` ${pkgs}\n` } nixfile = nixfile.trim() nixfile += ` ]; }\n` // Write `.default.nix` for use by Nix this.write('.default.nix', nixfile) return nixfile } }