Skip to content

Instantly share code, notes, and snippets.

@techieAgnostic
Created December 1, 2020 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techieAgnostic/02c1db811d1b02073c27371ce5f8c331 to your computer and use it in GitHub Desktop.
Save techieAgnostic/02c1db811d1b02073c27371ce5f8c331 to your computer and use it in GitHub Desktop.
Julia in Nix
{
description = "a nix flake for compiling julia programs";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let pkgs = nixpkgs.legacyPackages.${system};
in rec {
packages = {
juliaSkele = (import ./juliaDeri.nix) { pkgs = pkgs; pname = "myjulia"; };
};
defaultPackage = packages.juliaSkele;
}
);
}
{ pkgs,
pname,
exampleFile ? "src/Example.jl",
mainFile ? "src/Main.jl",
... }:
pkgs.stdenv.mkDerivation rec {
name = pname;
src = ./.;
buildInputs = with pkgs; [ julia curl ];
phases = [ "configurePhase" "buildPhase" "installPhase" ];
configurePhase = ''
julia --project --trace-compile=precompile.jl -q ${src}/${exampleFile};
'';
buildPhase = ''
unset packageList;
unset finalList;
unset prepedColon;
packageList=$(julia --project -E 'using Pkg; Pkg.status(mode=PKGMODE_MANIFEST)' | sed 1,2d | head -n -1 | sed 's/.*\]\ //g' | sed 's/\ .*//g')
prependColon=$(for p in $packageList; do echo ":"''${p}", "; done)
finalList="["''${prependColon::-2}"]"
julia --project -q -E \
using Pkg \
Pkg.add("https://github.com/JuliaLang/PackageCompiler.jl" rev="v1.2.3") \
Pkg.instantiate \
Pkg.activate \
using PackageCompiler \
create_sysimage(''${finalList}, sysimage_path="generatedSysImage.so", precompile_execution_file="$out/precompile.jl")
'';
installPhase = ''
rm $out/precompile.jl
mkdir -p $out/bin
cp -r * $out
echo "JULIA_LOAD_PATH=$out julia --project --sysimage ../generatedSysImage.so $out/${mainFile}" >> $out/bin/${pname}
chmod +x $out/bin/${pname}
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment