aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/apps/memcached/default.nix67
-rw-r--r--modules/apps/memcached/instance.nix71
2 files changed, 138 insertions, 0 deletions
diff --git a/modules/apps/memcached/default.nix b/modules/apps/memcached/default.nix
new file mode 100644
index 0000000..e963518
--- /dev/null
+++ b/modules/apps/memcached/default.nix
@@ -0,0 +1,67 @@
+{ config, lib, pkgs, ... }:
+
+let
+
+ inherit (builtins)
+ elem filter isBool isList ;
+
+ inherit (lib)
+ concatMapStringsSep concatStringsSep filterAttrs flatten foldAttrs foldl
+ mapAttrsToList mkOption optionalString ;
+
+ inherit (lib.types)
+ attrsOf submodule ;
+
+ concatMapAttrsSep = s: f: attrs: concatStringsSep s (mapAttrsToList f attrs);
+
+ explicit = filterAttrs (n: v: n != "_module" && v != null);
+ instances = explicit config.nixsap.apps.memcached;
+ users = mapAttrsToList (_: i: i.user) instances;
+
+
+ mkService = name: cfg:
+ let
+
+ show = n: v:
+ if isList v then map (s: "-${n} '${toString s}'") v
+ else if isBool v then (optionalString v "-${n}")
+ else "-${n} '${toString v}'";
+
+ args = flatten (mapAttrsToList show (explicit cfg.args));
+
+ start = pkgs.writeBashScriptBin "memcached-${name}-start" ''
+ set -euo pipefail
+ umask 0027
+
+ exec ${cfg.package}/bin/memcached \
+ ${concatStringsSep " \\\n" args}
+ '';
+
+ in {
+ "memcached-${name}" = {
+ description = "memcached (${name})";
+ wantedBy = [ "multi-user.target" ];
+ after = [ "keys.target" "network.target" "local-fs.target" ];
+ serviceConfig = {
+ ExecStart = "${start}/bin/memcached-${name}-start";
+ Restart = "always";
+ User = cfg.user;
+ };
+ };
+ };
+
+in {
+
+ options.nixsap.apps.memcached = mkOption {
+ description = "Memcached instances";
+ default = {};
+ type = attrsOf (submodule (import ./instance.nix pkgs));
+ };
+
+ config = {
+ systemd.services = foldl (a: b: a//b) {} (mapAttrsToList mkService instances);
+ nixsap.system.users.daemons = users;
+ };
+
+}
+
diff --git a/modules/apps/memcached/instance.nix b/modules/apps/memcached/instance.nix
new file mode 100644
index 0000000..102a287
--- /dev/null
+++ b/modules/apps/memcached/instance.nix
@@ -0,0 +1,71 @@
+pkgs:
+{ lib, name, ... }:
+
+let
+
+ inherit (builtins) match ;
+
+ inherit (lib)
+ mkOption mkOptionType ;
+
+ inherit (lib.types)
+ bool either enum int listOf nullOr package path str submodule ;
+
+ default = v: type: mkOption { type = type; default = v; };
+ optional = type: mkOption { type = nullOr type; default = null; };
+
+ isFloat = x: match "^[0-9]+(\\.[0-9]+)?$" (toString x) != null;
+
+ float = mkOptionType {
+ name = "positive float";
+ check = isFloat;
+ };
+
+in {
+ options = {
+
+ user = mkOption {
+ description = "User to run as";
+ type = str;
+ default = "memcached-${name}";
+ };
+
+ package = mkOption {
+ description = "Memcached package";
+ type = package;
+ default = pkgs.memcached;
+ };
+
+ args = mkOption {
+ description = "Memcached command line arguments. Refer to memcached man page.";
+ default = {};
+ type = submodule {
+ options = {
+ M = optional bool;
+ R = optional int;
+ B = optional (enum ["auto" "ascii" "binary"]);
+ I = optional int;
+ L = optional bool;
+ l = default "127.0.0.1" (either str (listOf str));
+ b = optional int;
+ c = optional int;
+ f = optional float;
+ p = default 11211 int;
+ t = optional int;
+ D = optional str;
+ a = optional str;
+ m = optional int;
+ n = optional int;
+ F = optional bool;
+ U = default 11211 int;
+ C = optional bool;
+ k = optional bool;
+ A = optional bool;
+ S = optional bool;
+ s = optional path;
+ };
+ };
+ };
+ };
+}
+