style: Format nix

nixfmt-rfc-style for flake
nixpkgs-fmt for nix/ directory
This commit is contained in:
Shuroii 2025-07-03 12:47:22 +02:00 committed by Jade Ellis
parent 0e2fdc415c
commit 09199b0ea7
No known key found for this signature in database
GPG key ID: 8705A2A3EBF77BD2
6 changed files with 780 additions and 725 deletions

1118
flake.nix

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
{ inputs { inputs
# Dependencies # Dependencies
, main , main
, mdbook , mdbook
, stdenv , stdenv

View file

@ -16,24 +16,24 @@ let
all_features = true; all_features = true;
disable_release_max_log_level = true; disable_release_max_log_level = true;
disable_features = [ disable_features = [
# console/CLI stuff isn't used or relevant for complement # console/CLI stuff isn't used or relevant for complement
"console" "console"
"tokio_console" "tokio_console"
# sentry telemetry isn't useful for complement, disabled by default anyways # sentry telemetry isn't useful for complement, disabled by default anyways
"sentry_telemetry" "sentry_telemetry"
"perf_measurements" "perf_measurements"
# this is non-functional on nix for some reason # this is non-functional on nix for some reason
"hardened_malloc" "hardened_malloc"
# dont include experimental features # dont include experimental features
"experimental" "experimental"
# compression isn't needed for complement # compression isn't needed for complement
"brotli_compression" "brotli_compression"
"gzip_compression" "gzip_compression"
"zstd_compression" "zstd_compression"
# complement doesn't need hot reloading # complement doesn't need hot reloading
"conduwuit_mods" "conduwuit_mods"
# complement doesn't have URL preview media tests # complement doesn't have URL preview media tests
"url_preview" "url_preview"
]; ];
}; };
@ -68,11 +68,12 @@ dockerTools.buildImage {
"${lib.getExe start}" "${lib.getExe start}"
]; ];
Entrypoint = if !stdenv.hostPlatform.isDarwin Entrypoint =
if !stdenv.hostPlatform.isDarwin
# Use the `tini` init system so that signals (e.g. ctrl+c/SIGINT) # Use the `tini` init system so that signals (e.g. ctrl+c/SIGINT)
# are handled as expected # are handled as expected
then [ "${lib.getExe' tini "tini"}" "--" ] then [ "${lib.getExe' tini "tini"}" "--" ]
else []; else [ ];
Env = [ Env = [
"CONTINUWUITY_TLS__KEY=${./private_key.key}" "CONTINUWUITY_TLS__KEY=${./private_key.key}"
@ -82,8 +83,8 @@ dockerTools.buildImage {
]; ];
ExposedPorts = { ExposedPorts = {
"8008/tcp" = {}; "8008/tcp" = { };
"8448/tcp" = {}; "8448/tcp" = { };
}; };
}; };
} }

View file

@ -4,51 +4,52 @@
, stdenv , stdenv
}: }:
lib.optionalAttrs stdenv.hostPlatform.isStatic { lib.optionalAttrs stdenv.hostPlatform.isStatic
ROCKSDB_STATIC = ""; {
} ROCKSDB_STATIC = "";
}
// //
{ {
CARGO_BUILD_RUSTFLAGS = CARGO_BUILD_RUSTFLAGS =
lib.concatStringsSep lib.concatStringsSep
" " " "
([] ([ ]
# This disables PIE for static builds, which isn't great in terms # This disables PIE for static builds, which isn't great in terms
# of security. Unfortunately, my hand is forced because nixpkgs' # of security. Unfortunately, my hand is forced because nixpkgs'
# `libstdc++.a` is built without `-fPIE`, which precludes us from # `libstdc++.a` is built without `-fPIE`, which precludes us from
# leaving PIE enabled. # leaving PIE enabled.
++ lib.optionals ++ lib.optionals
stdenv.hostPlatform.isStatic stdenv.hostPlatform.isStatic
[ "-C" "relocation-model=static" ] [ "-C" "relocation-model=static" ]
++ lib.optionals ++ lib.optionals
(stdenv.buildPlatform.config != stdenv.hostPlatform.config) (stdenv.buildPlatform.config != stdenv.hostPlatform.config)
[ [
"-l" "-l"
"c" "c"
"-l" "-l"
"stdc++" "stdc++"
"-L" "-L"
"${stdenv.cc.cc.lib}/${stdenv.hostPlatform.config}/lib" "${stdenv.cc.cc.lib}/${stdenv.hostPlatform.config}/lib"
] ]
); );
} }
# What follows is stolen from [here][0]. Its purpose is to properly # What follows is stolen from [here][0]. Its purpose is to properly
# configure compilers and linkers for various stages of the build, and # configure compilers and linkers for various stages of the build, and
# even covers the case of build scripts that need native code compiled and # even covers the case of build scripts that need native code compiled and
# run on the build platform (I think). # run on the build platform (I think).
# #
# [0]: https://github.com/NixOS/nixpkgs/blob/nixpkgs-unstable/pkgs/build-support/rust/lib/default.nix#L48-L68 # [0]: https://github.com/NixOS/nixpkgs/blob/nixpkgs-unstable/pkgs/build-support/rust/lib/default.nix#L48-L68
// //
( (
let let
inherit (rust.lib) envVars; inherit (rust.lib) envVars;
in in
lib.optionalAttrs lib.optionalAttrs
(stdenv.targetPlatform.rust.rustcTarget (stdenv.targetPlatform.rust.rustcTarget
!= stdenv.hostPlatform.rust.rustcTarget) != stdenv.hostPlatform.rust.rustcTarget)
( (
let let
inherit (stdenv.targetPlatform.rust) cargoEnvVarTarget; inherit (stdenv.targetPlatform.rust) cargoEnvVarTarget;

View file

@ -12,144 +12,146 @@
, rust-jemalloc-sys , rust-jemalloc-sys
, stdenv , stdenv
# Options (keep sorted) # Options (keep sorted)
, all_features ? false , all_features ? false
, default_features ? true , default_features ? true
# default list of disabled features # default list of disabled features
, disable_features ? [ , disable_features ? [
# dont include experimental features # dont include experimental features
"experimental" "experimental"
# jemalloc profiling/stats features are expensive and shouldn't # jemalloc profiling/stats features are expensive and shouldn't
# be expected on non-debug builds. # be expected on non-debug builds.
"jemalloc_prof" "jemalloc_prof"
"jemalloc_stats" "jemalloc_stats"
# this is non-functional on nix for some reason # this is non-functional on nix for some reason
"hardened_malloc" "hardened_malloc"
# conduwuit_mods is a development-only hot reload feature # conduwuit_mods is a development-only hot reload feature
"conduwuit_mods" "conduwuit_mods"
] ]
, disable_release_max_log_level ? false , disable_release_max_log_level ? false
, features ? [] , features ? [ ]
, profile ? "release" , profile ? "release"
# rocksdb compiled with -march=haswell and target-cpu=haswell rustflag # rocksdb compiled with -march=haswell and target-cpu=haswell rustflag
# haswell is pretty much any x86 cpu made in the last 12 years, and # haswell is pretty much any x86 cpu made in the last 12 years, and
# supports modern CPU extensions that rocksdb can make use of. # supports modern CPU extensions that rocksdb can make use of.
# disable if trying to make a portable x86_64 build for very old hardware # disable if trying to make a portable x86_64 build for very old hardware
, x86_64_haswell_target_optimised ? false , x86_64_haswell_target_optimised ? false
}: }:
let let
# We perform default-feature unification in nix, because some of the dependencies # We perform default-feature unification in nix, because some of the dependencies
# on the nix side depend on feature values. # on the nix side depend on feature values.
crateFeatures = path: crateFeatures = path:
let manifest = lib.importTOML "${path}/Cargo.toml"; in let manifest = lib.importTOML "${path}/Cargo.toml"; in
lib.remove "default" (lib.attrNames manifest.features); lib.remove "default" (lib.attrNames manifest.features);
crateDefaultFeatures = path: crateDefaultFeatures = path:
(lib.importTOML "${path}/Cargo.toml").features.default; (lib.importTOML "${path}/Cargo.toml").features.default;
allDefaultFeatures = crateDefaultFeatures "${inputs.self}/src/main"; allDefaultFeatures = crateDefaultFeatures "${inputs.self}/src/main";
allFeatures = crateFeatures "${inputs.self}/src/main"; allFeatures = crateFeatures "${inputs.self}/src/main";
features' = lib.unique features' = lib.unique
(features ++ (features ++
lib.optionals default_features allDefaultFeatures ++ lib.optionals default_features allDefaultFeatures ++
lib.optionals all_features allFeatures); lib.optionals all_features allFeatures);
disable_features' = disable_features ++ lib.optionals disable_release_max_log_level ["release_max_log_level"]; disable_features' = disable_features ++ lib.optionals disable_release_max_log_level [ "release_max_log_level" ];
features'' = lib.subtractLists disable_features' features'; features'' = lib.subtractLists disable_features' features';
featureEnabled = feature : builtins.elem feature features''; featureEnabled = feature: builtins.elem feature features'';
enableLiburing = featureEnabled "io_uring" && !stdenv.hostPlatform.isDarwin; enableLiburing = featureEnabled "io_uring" && !stdenv.hostPlatform.isDarwin;
# This derivation will set the JEMALLOC_OVERRIDE variable, causing the # This derivation will set the JEMALLOC_OVERRIDE variable, causing the
# tikv-jemalloc-sys crate to use the nixpkgs jemalloc instead of building it's # tikv-jemalloc-sys crate to use the nixpkgs jemalloc instead of building it's
# own. In order for this to work, we need to set flags on the build that match # own. In order for this to work, we need to set flags on the build that match
# whatever flags tikv-jemalloc-sys was going to use. These are dependent on # whatever flags tikv-jemalloc-sys was going to use. These are dependent on
# which features we enable in tikv-jemalloc-sys. # which features we enable in tikv-jemalloc-sys.
rust-jemalloc-sys' = (rust-jemalloc-sys.override { rust-jemalloc-sys' = (rust-jemalloc-sys.override {
# tikv-jemalloc-sys/unprefixed_malloc_on_supported_platforms feature # tikv-jemalloc-sys/unprefixed_malloc_on_supported_platforms feature
unprefixed = true; unprefixed = true;
}).overrideAttrs (old: { }).overrideAttrs (old: {
configureFlags = old.configureFlags ++ configureFlags = old.configureFlags ++
# we dont need docs # we dont need docs
[ "--disable-doc" ] ++ [ "--disable-doc" ] ++
# we dont need cxx/C++ integration # we dont need cxx/C++ integration
[ "--disable-cxx" ] ++ [ "--disable-cxx" ] ++
# tikv-jemalloc-sys/profiling feature # tikv-jemalloc-sys/profiling feature
lib.optional (featureEnabled "jemalloc_prof") "--enable-prof" ++ lib.optional (featureEnabled "jemalloc_prof") "--enable-prof" ++
# tikv-jemalloc-sys/stats feature # tikv-jemalloc-sys/stats feature
(if (featureEnabled "jemalloc_stats") then [ "--enable-stats" ] else [ "--disable-stats" ]); (if (featureEnabled "jemalloc_stats") then [ "--enable-stats" ] else [ "--disable-stats" ]);
});
buildDepsOnlyEnv =
let
rocksdb' = (rocksdb.override {
jemalloc = lib.optional (featureEnabled "jemalloc") rust-jemalloc-sys';
# rocksdb fails to build with prefixed jemalloc, which is required on
# darwin due to [1]. In this case, fall back to building rocksdb with
# libc malloc. This should not cause conflicts, because all of the
# jemalloc symbols are prefixed.
#
# [1]: https://github.com/tikv/jemallocator/blob/ab0676d77e81268cd09b059260c75b38dbef2d51/jemalloc-sys/src/env.rs#L17
enableJemalloc = featureEnabled "jemalloc" && !stdenv.hostPlatform.isDarwin;
# for some reason enableLiburing in nixpkgs rocksdb is default true
# which breaks Darwin entirely
enableLiburing = enableLiburing;
}).overrideAttrs (old: {
enableLiburing = enableLiburing;
cmakeFlags = (if x86_64_haswell_target_optimised then (lib.subtractLists [
# dont make a portable build if x86_64_haswell_target_optimised is enabled
"-DPORTABLE=1"
] old.cmakeFlags
++ [ "-DPORTABLE=haswell" ]) else ([ "-DPORTABLE=1" ])
)
++ old.cmakeFlags;
# outputs has "tools" which we dont need or use
outputs = [ "out" ];
# preInstall hooks has stuff for messing with ldb/sst_dump which we dont need or use
preInstall = "";
});
in
{
# https://crane.dev/faq/rebuilds-bindgen.html
NIX_OUTPATH_USED_AS_RANDOM_SEED = "aaaaaaaaaa";
CARGO_PROFILE = profile;
ROCKSDB_INCLUDE_DIR = "${rocksdb'}/include";
ROCKSDB_LIB_DIR = "${rocksdb'}/lib";
}
//
(import ./cross-compilation-env.nix {
# Keep sorted
inherit
lib
pkgsBuildHost
rust
stdenv;
}); });
buildPackageEnv = { buildDepsOnlyEnv =
GIT_COMMIT_HASH = inputs.self.rev or inputs.self.dirtyRev or ""; let
GIT_COMMIT_HASH_SHORT = inputs.self.shortRev or inputs.self.dirtyShortRev or ""; rocksdb' = (rocksdb.override {
} // buildDepsOnlyEnv // { jemalloc = lib.optional (featureEnabled "jemalloc") rust-jemalloc-sys';
# Only needed in static stdenv because these are transitive dependencies of rocksdb # rocksdb fails to build with prefixed jemalloc, which is required on
CARGO_BUILD_RUSTFLAGS = buildDepsOnlyEnv.CARGO_BUILD_RUSTFLAGS # darwin due to [1]. In this case, fall back to building rocksdb with
+ lib.optionalString (enableLiburing && stdenv.hostPlatform.isStatic) # libc malloc. This should not cause conflicts, because all of the
# jemalloc symbols are prefixed.
#
# [1]: https://github.com/tikv/jemallocator/blob/ab0676d77e81268cd09b059260c75b38dbef2d51/jemalloc-sys/src/env.rs#L17
enableJemalloc = featureEnabled "jemalloc" && !stdenv.hostPlatform.isDarwin;
# for some reason enableLiburing in nixpkgs rocksdb is default true
# which breaks Darwin entirely
enableLiburing = enableLiburing;
}).overrideAttrs (old: {
enableLiburing = enableLiburing;
cmakeFlags = (if x86_64_haswell_target_optimised then
(lib.subtractLists [
# dont make a portable build if x86_64_haswell_target_optimised is enabled
"-DPORTABLE=1"
]
old.cmakeFlags
++ [ "-DPORTABLE=haswell" ]) else ([ "-DPORTABLE=1" ])
)
++ old.cmakeFlags;
# outputs has "tools" which we dont need or use
outputs = [ "out" ];
# preInstall hooks has stuff for messing with ldb/sst_dump which we dont need or use
preInstall = "";
});
in
{
# https://crane.dev/faq/rebuilds-bindgen.html
NIX_OUTPATH_USED_AS_RANDOM_SEED = "aaaaaaaaaa";
CARGO_PROFILE = profile;
ROCKSDB_INCLUDE_DIR = "${rocksdb'}/include";
ROCKSDB_LIB_DIR = "${rocksdb'}/lib";
}
//
(import ./cross-compilation-env.nix {
# Keep sorted
inherit
lib
pkgsBuildHost
rust
stdenv;
});
buildPackageEnv = {
GIT_COMMIT_HASH = inputs.self.rev or inputs.self.dirtyRev or "";
GIT_COMMIT_HASH_SHORT = inputs.self.shortRev or inputs.self.dirtyShortRev or "";
} // buildDepsOnlyEnv // {
# Only needed in static stdenv because these are transitive dependencies of rocksdb
CARGO_BUILD_RUSTFLAGS = buildDepsOnlyEnv.CARGO_BUILD_RUSTFLAGS
+ lib.optionalString (enableLiburing && stdenv.hostPlatform.isStatic)
" -L${lib.getLib liburing}/lib -luring" " -L${lib.getLib liburing}/lib -luring"
+ lib.optionalString x86_64_haswell_target_optimised + lib.optionalString x86_64_haswell_target_optimised
" -Ctarget-cpu=haswell"; " -Ctarget-cpu=haswell";
}; };
commonAttrs = { commonAttrs = {
inherit inherit
(craneLib.crateNameFromCargoToml { (craneLib.crateNameFromCargoToml {
cargoToml = "${inputs.self}/Cargo.toml"; cargoToml = "${inputs.self}/Cargo.toml";
}) })
pname pname
version; version;
src = let filter = inputs.nix-filter.lib; in filter { src = let filter = inputs.nix-filter.lib; in filter {
root = inputs.self; root = inputs.self;
@ -167,22 +169,22 @@ commonAttrs = {
cargoExtraArgs = "--no-default-features --locked " cargoExtraArgs = "--no-default-features --locked "
+ lib.optionalString + lib.optionalString
(features'' != []) (features'' != [ ])
"--features " + (builtins.concatStringsSep "," features''); "--features " + (builtins.concatStringsSep "," features'');
dontStrip = profile == "dev" || profile == "test"; dontStrip = profile == "dev" || profile == "test";
dontPatchELF = profile == "dev" || profile == "test"; dontPatchELF = profile == "dev" || profile == "test";
buildInputs = lib.optional (featureEnabled "jemalloc") rust-jemalloc-sys' buildInputs = lib.optional (featureEnabled "jemalloc") rust-jemalloc-sys'
# needed to build Rust applications on macOS # needed to build Rust applications on macOS
++ lib.optionals stdenv.hostPlatform.isDarwin [ ++ lib.optionals stdenv.hostPlatform.isDarwin [
# https://github.com/NixOS/nixpkgs/issues/206242 # https://github.com/NixOS/nixpkgs/issues/206242
# ld: library not found for -liconv # ld: library not found for -liconv
libiconv libiconv
# https://stackoverflow.com/questions/69869574/properly-adding-darwin-apple-sdk-to-a-nix-shell # https://stackoverflow.com/questions/69869574/properly-adding-darwin-apple-sdk-to-a-nix-shell
# https://discourse.nixos.org/t/compile-a-rust-binary-on-macos-dbcrossbar/8612 # https://discourse.nixos.org/t/compile-a-rust-binary-on-macos-dbcrossbar/8612
pkgsBuildHost.darwin.apple_sdk.frameworks.Security pkgsBuildHost.darwin.apple_sdk.frameworks.Security
]; ];
nativeBuildInputs = [ nativeBuildInputs = [
# bindgen needs the build platform's libclang. Apparently due to "splicing # bindgen needs the build platform's libclang. Apparently due to "splicing
@ -195,11 +197,11 @@ commonAttrs = {
# differing values for `NIX_CFLAGS_COMPILE`, which contributes to spurious # differing values for `NIX_CFLAGS_COMPILE`, which contributes to spurious
# rebuilds of bindgen and its depedents. # rebuilds of bindgen and its depedents.
jq jq
]; ];
}; };
in in
craneLib.buildPackage ( commonAttrs // { craneLib.buildPackage (commonAttrs // {
cargoArtifacts = craneLib.buildDepsOnly (commonAttrs // { cargoArtifacts = craneLib.buildDepsOnly (commonAttrs // {
env = buildDepsOnlyEnv; env = buildDepsOnlyEnv;
}); });
@ -208,8 +210,8 @@ craneLib.buildPackage ( commonAttrs // {
cargoExtraArgs = "--no-default-features --locked " cargoExtraArgs = "--no-default-features --locked "
+ lib.optionalString + lib.optionalString
(features'' != []) (features'' != [ ])
"--features " + (builtins.concatStringsSep "," features''); "--features " + (builtins.concatStringsSep "," features'');
env = buildPackageEnv; env = buildPackageEnv;

View file

@ -1,6 +1,6 @@
{ inputs { inputs
# Dependencies # Dependencies
, dockerTools , dockerTools
, lib , lib
, main , main
@ -17,11 +17,12 @@ dockerTools.buildLayeredImage {
main main
]; ];
config = { config = {
Entrypoint = if !stdenv.hostPlatform.isDarwin Entrypoint =
if !stdenv.hostPlatform.isDarwin
# Use the `tini` init system so that signals (e.g. ctrl+c/SIGINT) # Use the `tini` init system so that signals (e.g. ctrl+c/SIGINT)
# are handled as expected # are handled as expected
then [ "${lib.getExe' tini "tini"}" "--" ] then [ "${lib.getExe' tini "tini"}" "--" ]
else []; else [ ];
Cmd = [ Cmd = [
"${lib.getExe main}" "${lib.getExe main}"
]; ];
@ -31,7 +32,7 @@ dockerTools.buildLayeredImage {
Labels = { Labels = {
"org.opencontainers.image.authors" = "June Clementine Strawberry <june@girlboss.ceo> and Jason Volk "org.opencontainers.image.authors" = "June Clementine Strawberry <june@girlboss.ceo> and Jason Volk
<jason@zemos.net>"; <jason@zemos.net>";
"org.opencontainers.image.created" ="@${toString inputs.self.lastModified}"; "org.opencontainers.image.created" = "@${toString inputs.self.lastModified}";
"org.opencontainers.image.description" = "a very cool Matrix chat homeserver written in Rust"; "org.opencontainers.image.description" = "a very cool Matrix chat homeserver written in Rust";
"org.opencontainers.image.documentation" = "https://continuwuity.org/"; "org.opencontainers.image.documentation" = "https://continuwuity.org/";
"org.opencontainers.image.licenses" = "Apache-2.0"; "org.opencontainers.image.licenses" = "Apache-2.0";