Skip to content

Commit c499dd0

Browse files
sharadmvapaszke
authored andcommitted
Enable building Dex with Nix
1 parent a0b5067 commit c499dd0

File tree

7 files changed

+203
-10
lines changed

7 files changed

+203
-10
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ manually. This will be ignored by git.
8585

8686
This should work out of the box with Emacs' `lsp-haskell` package.
8787

88+
### Building with Nix
89+
90+
[Nix](https://nixos.org/) is a functional package manager and build system.
91+
92+
To build with vanilla Nix:
93+
```bash
94+
$ nix-build
95+
```
96+
97+
To build with flakes-enabled Nix:
98+
```bash
99+
$ nix build .#dex
100+
```
101+
The resulting `dex` binary should be in `result/bin/dex`.
102+
103+
For development purposes, you can use a Nix environment with
104+
```bash
105+
$ nix-shell
106+
$ nix develop # With flakes
107+
```
108+
and use `make` to use Stack to build Dex.
109+
88110
## Running
89111

90112
* Traditional REPL: `dex repl`

default.nix

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{ pkgs ? import <nixpkgs> {},
2+
llvm-hs-src ? pkgs.fetchFromGitHub {
3+
owner = "llvm-hs";
4+
repo = "llvm-hs";
5+
rev = "llvm-12";
6+
sha256 = "IG4Mh89bY+PtBJtzlXKYsPljfHP7OSQk03pV6fSmdRY=";
7+
},
8+
cudaPackage ? pkgs.cudaPackages.cudatoolkit_11,
9+
cuda ? false,
10+
optimized ? true,
11+
live ? true,
12+
}:
13+
let
14+
llvm-hs-pure = pkgs.haskellPackages.callCabal2nix "llvm-hs-pure" "${llvm-hs-src}/llvm-hs-pure" {
15+
};
16+
llvm-hs = (pkgs.haskellPackages.callCabal2nix "llvm-hs" "${llvm-hs-src}/llvm-hs" {
17+
inherit llvm-hs-pure;
18+
}).overrideAttrs (oldAttrs: rec {
19+
buildInputs = oldAttrs.buildInputs ++ [
20+
pkgs.llvm_12
21+
];
22+
});
23+
buildFlags = pkgs.lib.optionals optimized [
24+
"-foptimized"
25+
] ++ pkgs.lib.optionals live [
26+
"-flive"
27+
] ++ pkgs.lib.optionals cuda [
28+
"-fcuda"
29+
"--extra-include-dirs=${cudaPackage}/include"
30+
"--extra-lib-dirs=${cudaPackage}/lib64/stubs"
31+
];
32+
cxxFlags = [
33+
"-fPIC"
34+
"-std=c++11"
35+
"-fno-exceptions"
36+
"-fno-rtti"
37+
] ++ pkgs.lib.optional cuda "-DDEX_CUDA"
38+
++ pkgs.lib.optional live "-DDEX_LIVE";
39+
buildRuntimeCommand = ''
40+
${pkgs.clang_9}/bin/clang++ \
41+
${builtins.concatStringsSep " " cxxFlags} \
42+
-c \
43+
-emit-llvm \
44+
-I${pkgs.libpng}/include \
45+
src/lib/dexrt.cpp \
46+
-o src/lib/dexrt.bc
47+
'';
48+
in
49+
# `callCabal2nix` converts `dex.cabal` into a Nix file and builds it.
50+
# Before we do the Haskell build though, we need to first compile the Dex runtime
51+
# so it's properly linked in when compiling Dex. Normally the makefile does this,
52+
# so we instead sneak compiling the runtime in the configuration phase for the Haskell build.
53+
(pkgs.haskellPackages.callCabal2nix "dex" ./. {
54+
inherit llvm-hs;
55+
inherit llvm-hs-pure;
56+
}).overrideAttrs (attrs: {
57+
configurePhase = ''
58+
# Compile the Dex runtime
59+
echo 'Compiling the Dex runtime...'
60+
set -x
61+
${buildRuntimeCommand}
62+
set +x
63+
echo 'Done compiling the Dex runtime.'
64+
65+
# Run the Haskell configuration phase
66+
${attrs.configurePhase}
67+
'';
68+
configureFlags = builtins.concatStringsSep " " buildFlags;
69+
buildInputs = attrs.buildInputs ++ (pkgs.lib.optional cuda
70+
cudaPackage
71+
);
72+
})

flake.lock

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
description = "Dex (named for \"index\") is a research language for typed, functional array processing.";
3+
4+
inputs = {
5+
flake-utils.url = "github:numtide/flake-utils";
6+
llvm-hs-src = {
7+
url = "github:llvm-hs/llvm-hs/llvm-12";
8+
flake = false;
9+
};
10+
};
11+
12+
outputs = { self, nixpkgs, flake-utils, llvm-hs-src }:
13+
flake-utils.lib.eachDefaultSystem (system:
14+
let
15+
pkgs = (import nixpkgs {
16+
inherit system;
17+
config.allowUnfree = true; # Needed for CUDA
18+
});
19+
in rec {
20+
packages.dex = (pkgs.callPackage ./. {
21+
inherit pkgs;
22+
inherit llvm-hs-src;
23+
});
24+
packages.dex-cuda = (pkgs.callPackage ./. {
25+
inherit pkgs;
26+
inherit llvm-hs-src;
27+
withCudaSupport = true;
28+
});
29+
defaultPackage = packages.dex;
30+
31+
devShell = (import ./shell.nix {
32+
inherit pkgs;
33+
});
34+
});
35+
}

makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Set shell to bash to resolve symbolic links when looking up
22
# executables, to support user-account installation of stack.
3-
SHELL=/bin/bash
3+
SHELL=/usr/bin/env bash
44

55
STACK=$(shell command -v stack 2>/dev/null)
66
ifeq (, $(STACK))

shell.nix

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
{ nixpkgs ? import <nixpkgs> {} }:
2-
with nixpkgs;
3-
stdenv.mkDerivation {
1+
{ pkgs ? import <nixpkgs> {} }:
2+
pkgs.stdenv.mkDerivation {
43
name = "dex";
5-
buildInputs = [
4+
buildInputs = with pkgs; [
65
cabal-install
6+
cacert
7+
clang_12
8+
git
79
haskell.compiler.ghc884
8-
llvm_9
9-
clang_9
10-
pkg-config
1110
libpng
12-
git
13-
cacert
11+
llvm_12
12+
pkg-config
13+
stack
14+
zlib
1415
];
1516
}

stack.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ extra-deps:
2020
- store-0.7.8@sha256:0b604101fd5053b6d7d56a4ef4c2addf97f4e08fe8cd06b87ef86f958afef3ae,8001
2121
- store-core-0.4.4.4@sha256:a19098ca8419ea4f6f387790e942a7a5d0acf62fe1beff7662f098cfb611334c,1430
2222
- th-utilities-0.2.4.1@sha256:b37d23c8bdabd678aee5a36dd4373049d4179e9a85f34eb437e9cd3f04f435ca,1869
23+
24+
nix:
25+
enable: false
26+
packages: [ libpng llvm_12 pkg-config zlib ]

0 commit comments

Comments
 (0)