Skip to content

Commit 74ac9ba

Browse files
authored
Merge pull request #31 from charguer/nix-test
Nix flake installation option
2 parents 2f6a6d4 + 2dc1426 commit 74ac9ba

6 files changed

Lines changed: 191 additions & 12 deletions

File tree

.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ LOG_BIN
2929
*.exe
3030

3131
# generated by dune
32+
_opam
3233
*.opam
3334
*.install
3435

@@ -60,3 +61,4 @@ _doc
6061
.idea
6162
.vscode/action_out.txt
6263
.ocamlformat
64+
.direnv

INSTALL.md

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
# OptiTrust Installation
33

44
--------------------------------------------------------------------------------
5-
## Installation
5+
6+
The system installation instructions below are provided for systems running Ubuntu. There is an experimental option to install using a Nix flake, which contains the required opam environment and LLVM dependencies in an isolated environment, but it is known to have issues on systems with a newer libc (such as Arch Linux), and does not integrate well with VSCode/ocaml LSP. Instructions for the nix flake [are also provided below](#nix-flake-installation-experimental).
7+
8+
## System Installation
69

710
### Install system packages
811

@@ -33,7 +36,7 @@ sudo mv clang+llvm-15.0.0-x86_64-linux-gnu-rhel-8.4 /opt/clang-15.0.0
3336
Because LLVM does not ship C++ headers, you need to install them separately:
3437
```sh
3538
# Install for C++ headers support:
36-
sudo apt-get install libc++-dev libc++abi-15-dev
39+
sudo apt-get install libc++-15-dev libc++abi-15-dev
3740
```
3841

3942
Depending on your prior installation, you might need to add the newly installed version of clang/llvm-config to the path, then select it among all of your versions :
@@ -78,12 +81,12 @@ Installation of the opam switch with relevant packages (it seems that it must be
7881

7982
```sh
8083
opam init
81-
opam switch create 4.14.1+options --packages=ocaml-variants.4.14.1+options,ocaml-option-flambda
82-
opam pin add dune 3.18.0
83-
opam pin add menhirLib 20210419
84-
opam pin add pprint 20220103
85-
opam pin add conf-libclang.15
86-
opam pin add clangml 4.8.0 # -> continueanyway
84+
opam switch create 4.14.2+options --packages=ocaml-variants.4.14.2+options,ocaml-option-flambda
85+
opam pin add -y dune 3.18.2
86+
opam pin add -y menhirLib 20210419
87+
opam pin add -y pprint 20220103
88+
opam pin add -y conf-libclang 15 --no-depexts
89+
opam pin add -y clangml 4.8.0 --no-depexts --yes --no-action
8790
opam install dune refl pprint menhir menhirLib base64 ocamlbuild ocaml-lsp-server ppx_deriving
8891
```
8992

@@ -107,12 +110,40 @@ Install remaining packages:
107110
eval $(opam env)
108111
```
109112

110-
Note: dune 3.19.0 is known to have an issue, when executing the tester script use 3.18.0 instead
113+
Note: dune 3.19.0 is known to have an issue, when executing the tester script use 3.18.2 instead
111114

112115
Note: clangml 4.8.0 is from Sept 2022.
113116

114117
Note: graphics is used by the pview tool only.
115118

119+
Next, jump to the [Optitrust Setup](#optitrust-setup) section.
120+
121+
## Nix flake installation (experimental)
122+
123+
### Install Nix
124+
125+
First make sure Nix is installed on your system. Follow [the instructions for a multi-user Nix installation](https://nixos.org/download/). We recommend using this install script instead of whatever package your distro may provide for Nix.
126+
127+
### Enable Nix flakes
128+
129+
We use flakes, which are still an experimental Nix feature. Add this line to `~/.config/nix/nix.conf` (create the directory if it doesn't exist) to permanently enable the feature:
130+
131+
```
132+
experimental-features = nix-command flakes
133+
```
134+
135+
### Test that it works
136+
137+
In the root of the OptiTrust repo, run `nix develop`. If it works, this will drop you into a shell with the special OptiTrust Nix environment. Note that unlike e.g. a Docker container, the binaries and libraries of your system are still exposed to the environment. The environment simply shadows all of the packages you have installed on your system. In some cases, this will break things because something on your system may for example depend on a newer version of `libc` than the Nix environment has. Depending on your system's libc, you may be able to run the system VSCode inside this environment, otherwise you have to run nix develop inside the VS Code terminal.
138+
139+
The first time running `nix develop` will take a while as it is setting up all the system packages for the environment, as well as installing OCaml and all of OptiTrust's OCaml dependencies. If it is working, you should not see any errors from the output of all the `opam` commands that were run to set up the environment for the first time.
140+
141+
Note that any time you want to run commands related to OptiTrust (opam clang etc.), you will need to drop into this shell again with `nix develop`. After the first time, everything is cached so it is much faster.
142+
143+
Follow to the next section to test that your OptiTrust installation works.
144+
145+
## OptiTrust setup
146+
116147
### Install precommit hooks
117148

118149
This command configures git to automatically run unit tests between commits. It can be ignored if you just want to try OptiTrust without contributing, and you have not downloaded the source files through git.
@@ -146,7 +177,7 @@ If this works, try :
146177

147178
### Configure VScode (or VSCodium) for interactive usage
148179

149-
You can install either VSCode or VSCodium (more open).
180+
You can install either VSCode or VSCodium (more open). You should install this on the system even if you are using the Nix environment.
150181
To install VSCode, visit: https://code.visualstudio.com/docs/?dv=linux64_deb
151182

152183
Download the `.deb` package, then open it using "Software Install".
@@ -171,9 +202,25 @@ may want to add into your `~/.bashrc` the line:
171202
(or use `sudo ln -s /usr/bin/codium /usr/bin/code`).
172203

173204

205+
### Direnv setup for automatic Nix shell activation in VSCode (experimental)
206+
207+
If vscode works within the nix shell on your system, there is a vscode extension which can activate the shell automatically whenever you open the repo. That way you don't have to launch a terminal, do `nix develop`, and then run `code .` As with the nix shell your mileage may vary.
208+
209+
Note: all of this installation happens on your system.
210+
211+
1. [Install direnv](https://direnv.net/docs/installation.html). You don't have to add the shell hook as written on the website. Just do step 1.
212+
213+
2. [Install nix-direnv](https://github.com/nix-community/nix-direnv?tab=readme-ov-file#from-source). You are following the "from source" instructions here.
214+
215+
3. [Install direnv extension for VSCode](https://marketplace.visualstudio.com/items?itemName=mkhl.direnv).
216+
217+
Once installed, when you open the OptiTrust repo in VScode, you should first be prompted by the `direnv` extension in the bottom right to allow the `.envrc` in the repo to be run. Hit `Allow`. It should then ask you if you want to reload the window to activate the Nix environment. Once reloaded, the OCaml extension should also now be able to see the opam environment belonging to your Nix. Terminals you open in the editor will also now inherit the nix environment, so you don't have to type `nix develop` in them.
218+
174219
--------------------------------------------------------------------------------
175220
## Browser installation
176221

222+
NOTE: If you're using the Nix shell, don't try to install a browser in the Nix environment, install it on the system as you would normally.
223+
177224
Recommended: installation of Chromium browser, which is very fast for
178225
opening up the "diff" pages generated by OptiTrust:
179226

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ It is described in this draft paper:
55

66
http://www.chargueraud.org/research/2024/optitrust/optitrust.pdf
77

8-
The project webpage is:azrazerazerazerazer
8+
The project webpage is:
99

1010
http://optitrust.inria.fr
1111

1212
OptiTrust is a research prototype, under active development.
1313

1414
Warning: in the current version, generating fully detailed HTML reports (with information about every substep) for our case studies requires a very large amount of memory, possibly more than available on your machine. We are working on making typechecking incremental to avoid this problem.
1515

16-
If you are interested in a demo, please get in touch with @charguer. cicici
16+
If you are interested in a demo, please get in touch with @charguer.
1717

1818
# Steps for using OptiTrust
1919

flake.lock

Lines changed: 27 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: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{
2+
description = "OCaml dev environment with custom LLVM binary + opam switch";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11";
6+
};
7+
8+
outputs = { self, nixpkgs }:
9+
let
10+
system = "x86_64-linux";
11+
pkgs = import nixpkgs { inherit system; };
12+
13+
# ---- Custom LLVM binary derivation ----
14+
customLLVM = pkgs.stdenv.mkDerivation {
15+
pname = "optitrust_llvm";
16+
version = "15.0.0";
17+
18+
src = pkgs.fetchurl {
19+
url = "https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.0/clang+llvm-15.0.0-x86_64-linux-gnu-rhel-8.4.tar.xz";
20+
sha256 = "20b17fabc97b93791098e771adf18013c50eae2e45407f8bfa772883b6027d30";
21+
};
22+
23+
phases = [ "unpackPhase" "installPhase" ];
24+
25+
unpackPhase = ''
26+
mkdir source
27+
tar -xf $src -C source --strip-components=1
28+
'';
29+
30+
installPhase = ''
31+
mkdir -p $out
32+
cp -r source/* $out/
33+
34+
# Add version-suffixed symlink for llvm-config
35+
ln -sf $out/bin/llvm-config $out/bin/llvm-config-15
36+
'';
37+
};
38+
39+
dev = pkgs.mkShell {
40+
buildInputs = [
41+
pkgs.opam
42+
pkgs.ocaml
43+
pkgs.pkg-config
44+
pkgs.autoconf
45+
pkgs.curl
46+
pkgs.libev
47+
pkgs.ncurses
48+
customLLVM
49+
# pkgs.gcc.cc.lib # removed because it was causing problems
50+
];
51+
52+
# ---- Ensure custom LLVM is used ----
53+
LLVM_HOME = customLLVM;
54+
55+
# ---- Where opam root lives (inside repo) ----
56+
OPAMROOT = ".opam";
57+
58+
shellHook = ''
59+
export PATH="$LLVM_HOME/bin:$PATH"
60+
export LD_LIBRARY_PATH="$LLVM_HOME/lib:${pkgs.gcc.cc.lib}/lib:$LD_LIBRARY_PATH"
61+
export CPATH="$LLVM_HOME/include:$CPATH"
62+
63+
# Initialize isolated opam root (project local)
64+
if [ ! -d "$OPAMROOT" ]; then
65+
echo "[nix-shell] initializing opam root"
66+
opam init --bare --disable-sandboxing -n
67+
68+
# Create switch with your exact compiler variant
69+
opam switch create . --no-install \
70+
--packages=ocaml-variants.4.14.2+options,ocaml-option-flambda
71+
fi
72+
73+
eval "$(opam env --switch=.)"
74+
75+
# ---- Apply your pins (idempotent) ----
76+
# quit first if everything is already installed
77+
if [ ! "$(printf '%s\n' dune menhirLib pprint conf-libclang clangml refl menhir base64 ocamlbuild ocaml-lsp-server ppx_deriving dream odoc lambdasoup ocaml-lsp-server dot-merlin-reader | grep -xF -f <(opam list --short --installed) | wc -l)" -eq 16 ]; then
78+
opam pin add -y dune 3.18.2
79+
opam pin add -y menhirLib 20210419
80+
opam pin add -y pprint 20220103
81+
opam pin add -y conf-libclang 15 --no-depexts
82+
opam pin add -y clangml 4.8.0 --no-depexts --yes --no-action || true # continue anyway
83+
84+
# ---- Install your required packages ----
85+
opam install -y ocaml-lsp-server dot-merlin-reader clangml dune refl pprint menhir menhirLib base64 ocamlbuild --no-depexts \
86+
ocaml-lsp-server ppx_deriving
87+
opam install -y odoc lambdasoup dream --no-depexts
88+
89+
opam init --no
90+
fi
91+
92+
'';
93+
};
94+
95+
in {
96+
devShells.${system}.default = dev;
97+
98+
## ---- Add this so that `nix shell` works ----
99+
packages.${system}.default = dev;
100+
};
101+
}
102+

0 commit comments

Comments
 (0)