Skip to content

Instantly share code, notes, and snippets.

@corpix
Last active August 21, 2022 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corpix/cd945fa6e718ee5f6eede501fc2039b1 to your computer and use it in GitHub Desktop.
Save corpix/cd945fa6e718ee5f6eede501fc2039b1 to your computer and use it in GitHub Desktop.
Tarantool for NixOS
{ pkgs ? import <nixpkgs> {}, ... }: let
inherit (pkgs)
stdenv
fetchgit
;
tag = "2.10.1";
commit = "482d91c66";
# git describe --long ${version}
version = "${tag}-0-g${commit}";
in stdenv.mkDerivation {
name = "tarantool";
inherit version;
src = fetchgit {
url = "https://github.com/tarantool/tarantool";
rev = tag;
sha256 = "sha256-lX1QcX2x6PrXm+dEyFB8+YRhf1m3Pam7eSHYwlD+v9M=";
fetchSubmodules = true;
};
cmakeFlags = [
"-Wno-dev"
"-DCMAKE_BUILD_TYPE=RelWithDebInfo"
"-DTARANTOOL_VERSION=${version}"
"-DCMAKE_INSTALL_LIBDIR=lib"
"-DENABLE_DIST=ON"
"-DENABLE_LTO=ON"
"-DENABLE_BACKTRACE=ON"
"-DWITH_SYSTEMD=OFF"
"-DWITH_SYSVINIT=OFF"
"-DWITH_LOGROTATE=OFF"
];
nativeBuildInputs = with pkgs; [
git
cmake
readline
ncurses
openssl
icu
zlib
c-ares
nghttp2
makeWrapper
];
buildInputs = with pkgs; [
python3
python3Packages.pyyaml
python3Packages.gevent
python3Packages.six
];
postInstall = ''
rm -rf $out/{etc,var,include,lib}
wrapProgram $out/bin/tarantoolctl --prefix PATH : $out/bin
'';
}
{ config, lib, pkgs, ... }: let
inherit (lib)
mkIf
mkOption
mkDefault
types
concatStringsSep
listToAttrs
attrNames
mapAttrs
mapAttrs'
nameValuePair
setAttr
;
name = "tarantool";
cfg = config.services.${name};
mkInstanceDir = instance: "${cfg.dataDir}/${instance}";
mkUser = instance: "${name}-${instance}";
instances = attrNames cfg.instances;
in {
options.services.${name} = {
enable = mkOption {
description = "Whether to enable ${name}";
type = types.bool;
default = false;
};
dataDir = mkOption {
description = "Data directory for ${name}";
type = types.path;
default = "/var/lib/${name}";
};
instances = mkOption {
description = "Per-instance settings for ${name}";
type = types.attrsOf (types.submodule {
options = {
name = mkOption {
description = "Instance name";
type = types.str;
default = "";
};
script = mkOption {
description = "Lua script to run in this instance";
type = types.path;
};
};
});
apply = mapAttrs (instance: submodule: setAttr submodule "name" instance);
};
};
config = mkIf cfg.enable {
systemd.tmpfiles.rules = ["d '${cfg.dataDir}' 0755 root - - -"];
systemd.services = mapAttrs' (instance: submodule: nameValuePair "${name}@${instance}" {
description = "Instance ${instance} on ${name}";
wantedBy = ["multi-user.target"];
after = ["network.target"];
serviceConfig = {
Type = "simple";
ExecStart = concatStringsSep " " [
"${pkgs.tarantool}/bin/tarantool"
submodule.script
];
User = mkUser instance;
WorkingDirectory = mkInstanceDir instance;
OOMScoreAdjust = -1000;
LimitNOFILE = 131072;
TimeoutStartSec = "86400s";
TimeoutStopSec = "10s";
Restart = "on-failure";
RestartSec = "100ms";
};
}) cfg.instances;
environment.systemPackages = with pkgs; [
tarantool
];
users.groups = listToAttrs (map
(instance: nameValuePair (mkUser instance) {})
instances);
users.users = listToAttrs (map
(instance: nameValuePair (mkUser instance) {
home = mkInstanceDir instance;
group = mkUser instance;
createHome = true;
isSystemUser = true;
})
instances);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment