From 650ac68669a4ffef288fc3a25e0b96267633f7ac Mon Sep 17 00:00:00 2001 From: Tony Olagbaiye Date: Mon, 20 Apr 2020 05:51:06 +0100 Subject: [PATCH] nix: add default.nix and envrc/shell.nix --- .envrc | 8 ++++++ default.nix | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ shell.nix | 35 ++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 .envrc create mode 100644 default.nix create mode 100644 shell.nix diff --git a/.envrc b/.envrc new file mode 100644 index 000000000..4d1873eb8 --- /dev/null +++ b/.envrc @@ -0,0 +1,8 @@ +if type lorri &>/dev/null; then + echo "direnv: using lorri from PATH ($(type -p lorri))" + eval "$(lorri direnv)" +else + # fall back to using direnv's builtin nix support + # to prevent bootstrapping problems. + use nix +fi diff --git a/default.nix b/default.nix new file mode 100644 index 000000000..3cad48cf0 --- /dev/null +++ b/default.nix @@ -0,0 +1,79 @@ +{ rev ? "c7e0e9ed5abd0043e50ee371129fcb8640264fc4" +, sha256 ? "0c28mpvjhjc8kiwj2w8zcjsr2rayw989a1wnsqda71zpcyas3mq2" +, pkgs ? import (builtins.fetchTarball { inherit sha256; + url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz"; + }) { } + +, stdenv ? if useClang + then (if pkgs.stdenv.cc.isClang + then pkgs.stdenv + else pkgs.llvmPackages_latest.stdenv) + else (if pkgs.stdenv.cc.isGNU + then pkgs.stdenv + else pkgs.gcc.stdenv) +, lib ? pkgs.lib + +, debug ? false # Debug Build +, useClang ? false # Use Clang over GCC +, useJemalloc ? true # Use the Dynamic Memory Allocator +, withGraphicsMagick ? true # Allow Media Thumbnails +}: + +stdenv.mkDerivation rec { + pname = "matrix-construct"; + version = "development"; + + src = lib.cleanSource ./.; + + preAutoreconf = let + VERSION_COMMIT_CMD = "git rev-parse --short HEAD"; + VERSION_BRANCH_CMD = "git rev-parse --abbrev-ref HEAD"; + VERSION_TAG_CMD = "git describe --tags --abbrev=0 --dirty --always"; + VERSION_CMD = "git describe --tags --always"; + runWithGit = id: cmd: lib.removeSuffix "\n" (builtins.readFile (pkgs.runCommandNoCCLocal "construct-${id}" { + buildInputs = [ pkgs.git ]; + } "cd ${./.} && ${cmd} > $out")); + in '' + substituteInPlace configure.ac --replace "${VERSION_COMMIT_CMD}" "echo ${runWithGit "version-commit" VERSION_COMMIT_CMD}" + substituteInPlace configure.ac --replace "${VERSION_BRANCH_CMD}" "echo ${runWithGit "version-branch" VERSION_BRANCH_CMD}" + substituteInPlace configure.ac --replace "${VERSION_TAG_CMD}" "echo ${runWithGit "version-tag" VERSION_TAG_CMD}" + substituteInPlace configure.ac --replace "${VERSION_CMD}" "echo ${runWithGit "version" VERSION_CMD}" + ''; + + configureFlags = [ + "--enable-generic" + "--with-custom-branding=nix" + "--with-custom-version=dev" + "--with-boost-libdir=${pkgs.boost.out}/lib" + "--with-boost=${pkgs.boost.dev}" + "--with-magic-file=${pkgs.file}/share/misc/magic.mgc" + ] ++ lib.optional useJemalloc "--enable-jemalloc" + ++ lib.optional withGraphicsMagick [ + "--with-imagemagick-includes=${pkgs.graphicsmagick}/include/GraphicsMagick" + ] ++ lib.optional debug "--with-log-level=DEBUG"; + + preBuild = '' + make clean + ''; + + enableParallelBuilding = true; + + nativeBuildInputs = with pkgs; [ + autoreconfHook pkg-config + ] ++ lib.optional useClang llvmPackages_latest.llvm + ++ lib.optional useJemalloc jemalloc; + buildInputs = with pkgs; [ + libsodium openssl file boost gmp + (rocksdb.overrideAttrs (super: rec { + version = "5.16.6"; + src = pkgs.fetchFromGitHub { + owner = "facebook"; + repo = "rocksdb"; + rev = "v${version}"; + sha256 = "0yy09myzbi99qdmh2c2mxlddr12pwxzh66ym1y6raaqglrsmax66"; + }; + cmakeFlags = builtins.map (f: if f == "-DWITH_TOOLS=0" then "-DWITH_TOOLS=1" else f) super.cmakeFlags; + NIX_CFLAGS_COMPILE = "${super.NIX_CFLAGS_COMPILE} -Wno-error=redundant-move"; + })) + ] ++ lib.optional withGraphicsMagick graphicsmagick; +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 000000000..f6412131b --- /dev/null +++ b/shell.nix @@ -0,0 +1,35 @@ +{ pkgs ? import { } }: + +let + package = pkgs.callPackage ./. {}; +in pkgs.mkShell { + buildInputs = with pkgs; [ + libsodium openssl file boost gmp llvm + (rocksdb.overrideAttrs (super: rec { + version = "5.16.6"; + src = pkgs.fetchFromGitHub { + owner = "facebook"; + repo = "rocksdb"; + rev = "v${version}"; + sha256 = "0yy09myzbi99qdmh2c2mxlddr12pwxzh66ym1y6raaqglrsmax66"; + }; + NIX_CFLAGS_COMPILE = "${super.NIX_CFLAGS_COMPILE} -Wno-error=redundant-move"; + })) + zlib lz4 snappy + graphicsmagick + jemalloc + ]; + nativeBuildInputs = with pkgs; [ git autoconf automake libtool gcc clang cmake pkg-config ]; + + shellHook = '' + WORKDIR=$(mktemp -d) + export RUNTIME_DIRECTORY=$WORKDIR/runtime + export STATE_DIRECTORY=$WORKDIR/state + export CACHE_DIRECTORY=$WORKDIR/cache + export LOGS_DIRECTORY=$WORKDIR/logs + export CONFIGURATION_DIRECTORY=$WORKDIR/configuration + + mkdir -p $RUNTIME_DIRECTORY $STATE_DIRECTORY $LOGS_DIRECTORY + mkdir -p $CACHE_DIRECTORY $CONFIGURATION_DIRECTORY + ''; +}