Skip to content

Commit

Permalink
Add nix flake
Browse files Browse the repository at this point in the history
Signed-off-by: Aiden Fox Ivey <[email protected]>
  • Loading branch information
aidenfoxivey committed Nov 6, 2024
1 parent 189e306 commit 13d5c87
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 74 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ liboqs is an open source C library for quantum-safe cryptographic algorithms.
- [Linux and Mac](#linux-and-mac)
- [Windows](#windows)
- [Cross compilation](#cross-compilation)
- [Nix Flake](#nix-flake)
- [Documentation](#documentation)
- [Contributing](#contributing)
- [License](#license)
Expand Down Expand Up @@ -167,6 +168,44 @@ If you want to create Visual Studio build files, e.g., if not using `ninja`, be

You can cross compile liboqs for various platforms. Detailed information is available [in the Wiki](https://github.com/open-quantum-safe/liboqs/wiki/Platform-specific-notes-for-building-liboqs#cross-compiling).

### Nix Flake

Nix is a tool that makes builds and developer environments deterministic. With just Nix installed, you can install `liboqs` using one command.

Need to install Nix? Consider using the [Lix installer](https://git.lix.systems/lix-project/lix-installer), a modern, community-focused implementation of the Nix tool, available for Linux and MacOS.

#### Building with Nix

You can build this easily with `nix build github:open-quantum-safe/liboqs/` if you only need the artifact `liboqs.a`.

If you want to build from a local version of the code, clone the repository and then run one of the following commands (depending on which toolchain you want to use and style of library you want to build):

```bash
# build a shared library using gcc
nix build .#gcc.shared

# build a static library using gcc
nix build .#gcc.static

# build a shared library using clang
nix build .#clang.shared

# build a static library using clang
nix build .#clang.static
```

#### Running the developer shell

In order to run a developer shell with the specified version of each dependency, you can run `nix develop`:

```bash
# using clang toolchain
nix develop .#clang

# using gcc toolchain
nix develop .#gcc
```

## Documentation

More detailed information on building, optional build parameters, example applications, coding conventions and more can be found in the [wiki](https://github.com/open-quantum-safe/liboqs/wiki).
Expand Down
8 changes: 4 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

142 changes: 72 additions & 70 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,85 +1,87 @@
# To develop:
#
# nix develop
# cmake -B build -GNinja
# ninja -C build
#
# To build:
# nix build .#static
# nix build .#shared

{
inputs = {
# we need this to access nix packages
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
# we need this to build for each system
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
name = "liboqs";
version = "0.8.0";
src = ./.;
pkgs = nixpkgs.legacyPackages.${system};
outputs = {
self,
nixpkgs,
flake-utils,
}:
flake-utils.lib.eachDefaultSystem (system: let
name = "liboqs";
src = ./.;
pkgs = nixpkgs.legacyPackages.${system};

# function to create derivation with configurable shared lib option
mkLib = shared: pkgs.stdenv.mkDerivation {
inherit name src;
# Function to create compiler-specific package sets
mkPackageSet = compiler: let
# Override the stdenv to use the specified compiler
stdenv =
if compiler == "clang"
then pkgs.clangStdenv
else pkgs.stdenv;

# build-time dependencies
nativeBuildInputs = with pkgs; [
cmake
ninja
doxygen
pkg-config
graphviz
gcc
];
mkLib = shared:
stdenv.mkDerivation {
inherit name src;
nativeBuildInputs = with pkgs;
[cmake ninja doxygen pkg-config graphviz]
++ (
if compiler == "clang"
then [pkgs.clang]
else [pkgs.gcc]
);

# run-time dependencies
buildInputs = with pkgs; [
openssl
];
buildInputs = with pkgs; [openssl];

cmakeFlags = [
"-GNinja"
"-DOQS_DIST_BUILD=ON"
"-DOQS_BUILD_ONLY_LIB=ON"
"-DBUILD_SHARED_LIBS=${if shared then "ON" else "OFF"}"
"-DCMAKE_INSTALL_LIBDIR=lib"
"-DCMAKE_INSTALL_INCLUDEDIR=include"
"-DCMAKE_INSTALL_PREFIX=${placeholder "out"}"
"-DCMAKE_INSTALL_FULL_LIBDIR=${placeholder "out"}/lib"
"-DCMAKE_INSTALL_FULL_INCLUDEDIR=${placeholder "out"}/include"
];
};
in
{
# default package builds shared library
packages.default = mkLib true;
cmakeFlags = [
"-GNinja"
"-DOQS_DIST_BUILD=ON"
"-DOQS_BUILD_ONLY_LIB=ON"
"-DBUILD_SHARED_LIBS=${
if shared
then "ON"
else "OFF"
}"
"-DCMAKE_INSTALL_LIBDIR=lib"
"-DCMAKE_INSTALL_INCLUDEDIR=include"
"-DCMAKE_INSTALL_PREFIX=${placeholder "out"}"
"-DCMAKE_INSTALL_FULL_LIBDIR=${placeholder "out"}/lib"
"-DCMAKE_INSTALL_FULL_INCLUDEDIR=${placeholder "out"}/include"
];
};
in {
shared = mkLib true;
static = mkLib false;
};

packages = {
shared = mkLib true;
static = mkLib false;
};

devShells.default = pkgs.mkShell {
# use runtime and build time dependencies
inherit (self.packages.${system}.default)
nativeBuildInputs
buildInputs;
# Create development shell for specified compiler
mkDevShell = compiler: let
packageSet = mkPackageSet compiler;
in
pkgs.mkShell {
inherit (packageSet.shared) nativeBuildInputs buildInputs;

# development only packages
packages = with pkgs; [
astyle
];
# astyle formats C source code and alejandra formats nix source code
packages = with pkgs; [astyle alejandra];

# export compile commands to simplify
shellHook = ''
export CMAKE_EXPORT_COMPILE_COMMANDS=1
echo "Using ${compiler} toolchain"
'';
};
}
);
}
in {
packages = {
default = (mkPackageSet "gcc").shared; # default is gcc shared
gcc = mkPackageSet "gcc";
clang = mkPackageSet "clang";
};

# Development shells
devShells = {
default = mkDevShell "gcc";
gcc = mkDevShell "gcc";
clang = mkDevShell "clang";
};
});
}

0 comments on commit 13d5c87

Please sign in to comment.