Skip to content

Commit b69562b

Browse files
committed
nix: init flake
1 parent bcd4d41 commit b69562b

File tree

5 files changed

+170
-53
lines changed

5 files changed

+170
-53
lines changed

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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
description = "A flake for the construct matrix homeserver";
3+
4+
outputs = { self, nixpkgs }: let
5+
forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" "i686-linux" "aarch64-linux" "x86_64-darwin" ];
6+
in {
7+
8+
overlay = final: prev: rec {
9+
matrix-construct-source = let
10+
inherit (prev) lib linkFarm;
11+
srcFilter = n: t: (lib.hasSuffix ".cc" n || lib.hasSuffix ".h" n || lib.hasSuffix ".S" n
12+
|| lib.hasSuffix ".md" n || t == "directory");
13+
repo = lib.cleanSourceWith { filter = srcFilter; src = lib.cleanSource "./."; };
14+
15+
buildFileWith = root: name: type: rec {
16+
inherit name; file = "${root}/${name}";
17+
path = if type == "directory" then buildFarmFrom name file else "${file}";
18+
};
19+
buildFarm = root: lib.mapAttrsToList (buildFileWith root) (builtins.readDir root);
20+
buildFarmFrom = basename: root: linkFarm (lib.strings.sanitizeDerivationName basename) (buildFarm root);
21+
in buildFarmFrom "construct" self;
22+
23+
matrix-construct = prev.callPackage ./nix/package {
24+
rev = if self ? rev then self.rev else "development";
25+
source = matrix-construct-source;
26+
};
27+
};
28+
29+
packages = forAllSystems (system: let
30+
pkgs = nixpkgs.legacyPackages.${system};
31+
in self.overlay pkgs pkgs);
32+
33+
defaultPackage = forAllSystems (system: self.packages.${system}.matrix-construct);
34+
35+
nixosModules = {
36+
matrix-construct = import ./nix/module self;
37+
};
38+
};
39+
}

nix/module/default.nix

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
self: { config, system, pkgs, lib, ... }:
2+
3+
let
4+
cfg = config.services.matrix-construct;
5+
in {
6+
options.services.matrix-construct = with lib; {
7+
enable = mkEnableOption "the construct server";
8+
9+
useScreen = mkOption {
10+
type = types.bool;
11+
default = true;
12+
example = false;
13+
description = ''
14+
Run construct in screen for stdio access.
15+
'';
16+
};
17+
18+
setupUnbound = mkOption {
19+
type = types.bool;
20+
default = true;
21+
example = false;
22+
description = ''
23+
Setup default unbound forwardAddresses.
24+
'';
25+
};
26+
27+
extraArgs = mkOption {
28+
type = with types; listOf str;
29+
default = [];
30+
example = [ "-6" "--debug" ];
31+
description = ''
32+
Extra flags to pass to construct.
33+
'';
34+
};
35+
36+
package = mkOption {
37+
type = types.package;
38+
default = self.packages.${system}.matrix-construct;
39+
defaultText = "pkgs.matrix-construct";
40+
description = ''
41+
Guix package to use.
42+
'';
43+
};
44+
45+
server = mkOption {
46+
type = types.str;
47+
default = null;
48+
example = "matrix.example.org";
49+
description = ''
50+
Server configuration to run construct with.
51+
'';
52+
};
53+
};
54+
55+
config = lib.mkIf cfg.enable {
56+
environment.systemPackages = [ cfg.package ] ++ lib.optional cfg.useScreen pkgs.screen;
57+
58+
systemd.services.construct = {
59+
description = "Matrix Construct";
60+
wantedBy = [ "multi-user.target" ];
61+
62+
## bin/construct host.tld [servername]
63+
## Connect to screen
64+
## Wait for init, then press ctrl-c
65+
## Create listener with `net listen matrix * 8448 privkey.pem cert.pem chain.pem`
66+
## ..I used /var/lib/acme/xa0.uk/key.pem /(...)/xa0.uk/fullchain.pem /(...)/xa0.uk/fullchain.pem`
67+
## Route and test with https://matrix.org/federationtester/api/report?server_name=host.tld
68+
## Restart, or reload with `mod reload web_root`
69+
## Exit screen
70+
script = '' cd $STATE_DIRECTORY && exec ''
71+
+ (if cfg.useScreen then '' ${pkgs.screen}/bin/screen -D -m '' else "")
72+
+ '' ${cfg.package}/bin/construct ${cfg.server} ${lib.concatStringsSep " " cfg.extraArgs} '';
73+
74+
serviceConfig = {
75+
Restart = "on-failure";
76+
ConfigurationDirectory = "construct";
77+
RuntimeDirectory = "construct";
78+
StateDirectory = "construct"; # Todo: bootstrap
79+
LogsDirectory = "construct";
80+
StandardOutput = "syslog";
81+
StandardError = "syslog";
82+
TimeoutStopSec = "120";
83+
KillSignal = "SIGQUIT";
84+
};
85+
};
86+
87+
services.unbound.forwardAddresses = lib.mkIf cfg.setupUnbound [ "4.2.2.1" "4.2.2.2" "4.2.2.3" "4.2.2.4" "4.2.2.5" "4.2.2.6" ];
88+
};
89+
}

default.nix renamed to nix/package/default.nix

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
{ rev ? "c7e0e9ed5abd0043e50ee371129fcb8640264fc4"
2-
, sha256 ? "0c28mpvjhjc8kiwj2w8zcjsr2rayw989a1wnsqda71zpcyas3mq2"
3-
, pkgs ? import (builtins.fetchTarball { inherit sha256;
4-
url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz";
5-
}) { }
6-
7-
, stdenv ? if useClang
8-
then (if pkgs.stdenv.cc.isClang
9-
then pkgs.stdenv
10-
else pkgs.llvmPackages_latest.stdenv)
11-
else (if pkgs.stdenv.cc.isGNU
12-
then pkgs.stdenv
13-
else pkgs.gcc.stdenv)
14-
, lib ? pkgs.lib
1+
{ source, rev, pkgs, lib, stdenv ? if useClang
2+
then (if pkgs.stdenv.cc.isClang
3+
then pkgs.stdenv
4+
else pkgs.llvmPackages_latest.stdenv)
5+
else (if pkgs.stdenv.cc.isGNU
6+
then pkgs.stdenv
7+
else pkgs.gcc.stdenv)
158

169
, debug ? false # Debug Build
1710
, useClang ? false # Use Clang over GCC
@@ -21,34 +14,13 @@
2114

2215
let
2316
pname = "matrix-construct";
24-
version = "development";
25-
26-
source = let
27-
srcFilter = n: t: (lib.hasSuffix ".cc" n || lib.hasSuffix ".h" n || lib.hasSuffix ".S" n
28-
|| lib.hasSuffix ".md" n || t == "directory");
29-
repo = lib.cleanSourceWith { filter = srcFilter; src = lib.cleanSource ./.; };
30-
31-
buildFileWith = root: name: type: rec {
32-
inherit name; file = "${root}/${name}";
33-
path = if type == "directory" then buildFarmFrom name file else "${file}";
34-
};
35-
buildFarm = root: lib.mapAttrsToList (buildFileWith root) (builtins.readDir root);
36-
buildFarmFrom = basename: root: pkgs.linkFarm (lib.strings.sanitizeDerivationName basename) (buildFarm root);
37-
in buildFarmFrom "construct" repo;
17+
version = lib.substring 0 9 rev;
3818

3919
buildArgs = buildInputs: nativeBuildInputs: {
4020
inherit buildInputs nativeBuildInputs;
4121
preferLocalBuild = true;
4222
allowSubstitutes = false;
4323
};
44-
45-
VERSION_COMMIT_CMD = "git rev-parse --short HEAD";
46-
VERSION_BRANCH_CMD = "git rev-parse --abbrev-ref HEAD";
47-
VERSION_TAG_CMD = "git describe --tags --abbrev=0 --dirty --always --broken";
48-
VERSION_CMD = "git describe --tags --always --broken";
49-
runWithGit = id: cmd: lib.removeSuffix "\n" (builtins.readFile (pkgs.runCommandNoCCLocal "construct-${id}" {
50-
buildInputs = [ pkgs.git ];
51-
} "cd ${./.} && ${cmd} > $out"));
5224
in stdenv.mkDerivation rec {
5325
inherit pname version;
5426
src = source;
@@ -528,7 +500,7 @@ in stdenv.mkDerivation rec {
528500
libtool --tag=CXX --mode=link g++ -std=gnu++17 -ftls-model=initial-exec -pthread ${CXXOPTS} -version-info 3:2:0 \
529501
-Wl,--no-undefined-version -Wl,--weak-unresolved-symbols -Wl,--unresolved-symbols=ignore-in-shared-libs \
530502
-Wl,--wrap=pthread_create -Wl,--wrap=pthread_join -Wl,--wrap=pthread_timedjoin_np -Wl,--wrap=pthread_self -Wl,--wrap=pthread_setname_np \
531-
-Wl,-z,nodelete -Wl,-z,nodlopen -Wl,-z,lazy -L${boost.out}/lib \
503+
-Wl,-z,nodelete -Wl,-z,nodlopen -Wl,-z,lazy -L${pkgs.boost.out}/lib \
532504
-Wl,-fuse-ld=gold -Wl,--gdb-index -Wl,--warn-common -Wl,--warn-execstack -Wl,--detect-odr-violations -Wl,--rosegment -Wl,-z,noexecstack -Wl,-z,combreloc -Wl,-z,text-unlikely-segment \
533505
-o $out/${laFile} ${lib.concatStringsSep " " loFiles} ${extraArgs} \
534506
-lrocksdb -lboost_coroutine -lboost_context -lboost_thread -lboost_filesystem -lboost_chrono -lboost_system -lssl -lcrypto -L${pkgs.libsodium.out}/lib -lsodium -lmagic -lz -lpthread -latomic -lrocksdb -ldl
@@ -570,11 +542,11 @@ in stdenv.mkDerivation rec {
570542

571543
versionDefs = let
572544
versions = {
573-
BRANDING_VERSION = "${runWithGit "version" VERSION_CMD}";
574-
RB_VERSION = "${runWithGit "version" VERSION_CMD}";
575-
RB_VERSION_BRANCH = "${runWithGit "version-branch" VERSION_BRANCH_CMD}";
576-
RB_VERSION_COMMIT = "${runWithGit "version-commit" VERSION_COMMIT_CMD}";
577-
RB_VERSION_TAG = "${runWithGit "version-tag" VERSION_TAG_CMD}";
545+
BRANDING_VERSION = lib.substring 0 9 rev;
546+
RB_VERSION = lib.substring 0 9 rev;
547+
RB_VERSION_BRANCH = "master";
548+
RB_VERSION_COMMIT = rev;
549+
RB_VERSION_TAG = rev;
578550
};
579551
in lib.concatStringsSep " " (lib.mapAttrsToList (k: v: "-U${k} -D'${k}=\"${v}\"'") versions);
580552

shell.nix

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,7 @@ let
2020
'';
2121
in pkgs.mkShell {
2222
buildInputs = with pkgs; [
23-
libsodium openssl file boost gmp llvm
24-
(rocksdb.overrideAttrs (super: rec {
25-
version = "5.16.6";
26-
src = pkgs.fetchFromGitHub {
27-
owner = "facebook";
28-
repo = "rocksdb";
29-
rev = "v${version}";
30-
sha256 = "0yy09myzbi99qdmh2c2mxlddr12pwxzh66ym1y6raaqglrsmax66";
31-
};
32-
NIX_CFLAGS_COMPILE = "${super.NIX_CFLAGS_COMPILE} -Wno-error=redundant-move";
33-
}))
23+
libsodium openssl file boost gmp llvm rocksdb
3424
zlib lz4 snappy
3525
graphicsmagick
3626
jemalloc

0 commit comments

Comments
 (0)