| 1 | ### Nix support |
| 2 | |
| 3 | From the [wiki](https://nixos.wiki/wiki/Nix_package_manager), Nix is a package manager and build system that parses reproducible build instructions specified in the Nix Expression Language. |
| 4 | |
| 5 | In the context of s2n-tls, we're using it to ease the setup of development environments that |
| 6 | closely match our CI. While this could be done with custom Docker containers, there are still missing toolchain issues to sort our per distribution and platform. Nix |
| 7 | does not replace or negate Docker, cmake and compilers, but should allow for frictionless installs of a wider number of versions of build and test dependencies. |
| 8 | |
| 9 | ### Quickstart |
| 10 | |
| 11 | - `sudo bash -c "mkdir /nix && chmod 755 /nix && chown -R $USERNAME /nix"` |
| 12 | - Run the single-user command from `https://nixos.org/download.html#nix-install-linux` |
| 13 | - Enable flakes: `mkdir ~/.config/nix; echo "experimental-features = nix-command flakes" > ~/.config/nix/nix.conf` |
| 14 | - `cd s2n-tls` |
| 15 | |
| 16 | #### What is this doing? |
| 17 | |
| 18 | 1. Creates a /nix store directory where artifacts (_derivations_) will be stored. It's not advised to change this, even for small roots - if you have disk space constraints, consider using a [bindmount](https://docs.rackspace.com/support/how-to/bind-mounts-in-linux/) |
| 19 | 2. Installs nix |
| 20 | 3. Enables the experimental _flakes_ feature. A flake is simply a source tree (such as a Git repository) containing a file named flake.nix that provides a standardized interface to Nix artifacts such as packages or NixOS modules. See the [blog post](https://www.tweag.io/blog/2020-05-25-flakes/) for more. |
| 21 | 4. cd into the s2n-tls project root, where flake.nix lives. |
| 22 | |
| 23 | ### Devshell |
| 24 | |
| 25 | A devShell is an environment with all of the build dependencies installed (on PATH) and ready for use. It is intended to easily get a usable development environment setup. |
| 26 | |
| 27 | To enter the development shell, run `nix develop` at the root of the project. |
| 28 | |
| 29 | There are some helper scripts in the environment to make building easier, but if you're familiar with Nix, note that these are |
| 30 | separate from the buildPhase, configurePhase and checkPhase. |
| 31 | |
| 32 | ### Specific libcrypto |
| 33 | |
| 34 | By default, the devShell uses Openssl-3. To run the devShell with a different libcrypto like awslc, use `nix develop .#awslc`. The currently supported options are awslc, openssl111, openssl102, and libressl. See flake.nix in the root directory. |
| 35 | |
| 36 | ### Rust development |
| 37 | |
| 38 | For Rust development, dedicated devshells are available that include the full Rust toolchain (rustc, cargo, clang, libclang) in addition to the standard development tools. These are accessible with the `rust_` prefix: |
| 39 | |
| 40 | ```bash |
| 41 | # Rust + OpenSSL variants |
| 42 | nix develop .#rust_openssl102 |
| 43 | nix develop .#rust_openssl111 |
| 44 | nix develop .#rust_openssl30 |
| 45 | |
| 46 | # Rust + AWS-LC variants |
| 47 | nix develop .#rust_awslc |
| 48 | nix develop .#rust_awslcfips2024 |
| 49 | ``` |
| 50 | |
| 51 | These rust-enabled shells provide everything needed for Rust integration testing and development, including proper libclang discovery for bindgen. The shell prompt will indicate the rust environment with `[nix rust <crypto-lib>]`. |
| 52 | |
| 53 | **Note**: Rust shells use dedicated functions (`rust_configure`, `rust_build`, `rust_test`) instead of the standard ones, as they require different CMake configuration (e.g., `S2N_INTERN_LIBCRYPTO=ON`, `BUILD_TESTING=OFF`). |
| 54 | |
| 55 | ### Configure and build |
| 56 | |
| 57 | From inside the devShell: `configure; build`. |
| 58 | |
| 59 | For Rust development, use the dedicated rust functions from within a rust-enabled devshell: `rust_configure; rust_build`. |
| 60 | |
| 61 | The first time this is run, it might take a while to build everything. |
| 62 | |
| 63 | ### Unit tests |
| 64 | |
| 65 | From inside the devShell after `configure` finishes, run `unit <test name>`, or with no test name for all of the tests. |
| 66 | For example, to run the stuffer_test use: `unit stuffer_test`, or `unit stuffer` to run all of tests with stuffer in the name. |
| 67 | |
| 68 | The CI does this in one shot with: `nix develop --max-jobs auto --ignore-environment --command bash -c "source ./nix/shell.sh; configure;unit" `. What this command is doing: |
| 69 | 1. max-jobs tells nix to use all the cores available to build |
| 70 | 2. ignore-environment strips out environment variables to get a clean environment |
| 71 | 3. source the shell functions needed to configure, build and run tests |
| 72 | |
| 73 | ### Integration tests |
| 74 | |
| 75 | From inside a devShell after running `configure` and `build`, use `uvinteg <test name>` to run the integration tests matching the regex `<test name>`, or with no arguments to run all the integration tests. Note that some of the tests are still broken under nix, so some failures are expected. |
| 76 | For example: `uvinteg happy_path`. |
| 77 | |
| 78 | The CI does this in one shot with `nix develop --max-jobs auto --ignore-environnment --command bash -c "source ./nix/shell.sh; configure;build;uvinteg" ` |
| 79 | |
| 80 | Like with the unit tests, an individual test, like [happy_path](https://github.com/aws/s2n-tls/blob/main/tests/integrationv2/test_happy_path.py) in this example, can be run with: `nix develop --max-jobs auto --ignore-environnment --command bash -c "source ./nix/shell.sh; configure;build;uvinteg happy_path"` |
| 81 | |
| 82 | For Rust integration tests, use `rust_test` from within a rust-enabled devshell after running `rust_configure` and `rust_build`. |
| 83 | |
| 84 | ### S3 Binary Cache |
| 85 | |
| 86 | Nix can store build artifacts in an external store, to reduce build times, and to allow CI to only do the build task once. |
| 87 | While there are [services to handle this](https://www.cachix.org/), for s2n-tls' CI, we're relying on S3 buckets. |
| 88 | |
| 89 | In its simplest form, the `nix copy` command can be used to stash a specific package, but in the case of CI, where we'd like to stash an entire build environment, |
| 90 | more sophistication is required. |
| 91 | |
| 92 | By using inputDerivation, we can create a meta-package that contains all the packages in our devShell. |
| 93 | |
| 94 | As an example, this copy will stash the s2n-tls devShell: |
| 95 | |
| 96 | ``` |
| 97 | nix copy --to 's3://my-nix-cache-bucket?region=us-west-2' .#devShell |
| 98 | ``` |
| 99 | |
| 100 | To retrieve these: |
| 101 | |
| 102 | ``` |
| 103 | nix copy --from 's3://my-nix-cache-bucket?region=us-west-2' --all --no-check-sigs |
| 104 | ``` |
| 105 | |
| 106 | (--no-check-sigs because this bucket is private and authenticated) |
| 107 | |
| 108 | #### Links |
| 109 | |
| 110 | - nix copy [documentation](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-copy.html) |