From 98c7bf1d0ceed62484a297d2b2561fc66123b832 Mon Sep 17 00:00:00 2001 From: Astro Date: Tue, 27 Oct 2020 19:20:34 +0100 Subject: [PATCH] nixos-module.nix: add --- nixos-module.nix | 115 ++++++++++++++++++++++++++++++++++++++ ticker-serve/src/main.rs | 4 +- ticker-update/src/main.rs | 2 +- 3 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 nixos-module.nix diff --git a/nixos-module.nix b/nixos-module.nix new file mode 100644 index 0000000..e85e702 --- /dev/null +++ b/nixos-module.nix @@ -0,0 +1,115 @@ +{ pkgs, config, lib, ... }: + +with import ./. { pkgs = import ../../programs/nixpkgs {}; }; + +let + inherit (pkgs.lib) mkOption types; + cfg = config.services.ticker; + defaultTickerConfig = { + db_url = "postgres:///ticker"; + calendars = {}; + weekdays = [ + "Montag" "Dienstag" "Mittwoch" "Donnerstag" + "Freitag" "Sonnabend" "Sonntag" + ]; + months = [ + "Januar" "Februar" "März" "April" + "Mai" "Juni" "Juli" "August" + "September" "Oktober" "November" "Dezember" + ]; + }; + tickerConfig = defaultTickerConfig // cfg.config; + configFile = pkgs.writeText "config.yaml" (lib.generators.toYAML {} tickerConfig); + workDir = pkgs.runCommandLocal "ticker-env" {} '' + mkdir $out + ln -s ${ticker-serve}/shared/ticker-serve/static $out/ + ln -s ${configFile} $out/config.yaml + ''; +in +{ + options.services.ticker = { + stateDir = mkOption { + type = types.str; + default = "/var/lib/ticker"; + description = '' + Directory where ticker files will be placed by default. + ''; + }; + user = mkOption { + type = types.str; + default = "ticker"; + description = "User account under which ticker runs."; + }; + group = mkOption { + type = types.str; + default = "ticker"; + description = "Group account under which ticker runs."; + }; + config = mkOption { + type = types.attrs; + default = defaultTickerConfig; + }; + updateInterval = mkOption { + type = types.str; + default = "1h"; + description = "Timer interval"; + }; + }; + + config = { + users.users.${cfg.user} = { + inherit (cfg) group; + }; + users.groups.${cfg.group} = {}; + + services.postgresql = { + enable = true; + }; + + systemd.services.ticker-setup = { + wantedBy = [ "multi-user.target" ]; + after = [ "postgresql.service" ]; + preStart = let + pgsu = config.services.postgresql.superUser; + psql = config.services.postgresql.package; + in '' + mkdir -p ${cfg.stateDir} + chown ${cfg.user}:${cfg.group} -R ${cfg.stateDir} + if ! test -e "${cfg.stateDir}/db-created"; then + ${pkgs.sudo}/bin/sudo -u ${pgsu} ${psql}/bin/createuser --no-superuser --no-createdb --no-createrole ${cfg.user} + ${pkgs.sudo}/bin/sudo -u ${pgsu} ${psql}/bin/createdb --owner ${cfg.user} ticker + ${pkgs.sudo}/bin/sudo -u ${cfg.user} ${psql}/bin/psql -f ${ticker-serve}/shared/libticker/schema.sql ticker + touch "${cfg.stateDir}/db-created" + fi + ''; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${pkgs.coreutils}/bin/test -f ${cfg.stateDir}/db-created"; + }; + }; + + systemd.services.ticker-update = { + wantedBy = [ "multi-user.target" ]; + after = [ "postgresql.service" "ticker-setup.service" ]; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${ticker-update}/bin/ticker-update"; + WorkingDirectory = "${workDir}"; + User = cfg.user; + Group = cfg.group; + }; + }; + + systemd.services.ticker-serve = { + wantedBy = [ "multi-user.target" ]; + after = [ "postgresql.service" "ticker-setup.service" ]; + serviceConfig = { + Type = "simple"; + ExecStart = "${ticker-serve}/bin/ticker-serve"; + WorkingDirectory = "${workDir}"; + User = cfg.user; + Group = cfg.group; + }; + }; + }; +} diff --git a/ticker-serve/src/main.rs b/ticker-serve/src/main.rs index c45ae85..c75596c 100644 --- a/ticker-serve/src/main.rs +++ b/ticker-serve/src/main.rs @@ -24,7 +24,7 @@ pub struct AppState { } fn main() { - let config = Config::read_yaml_file("../config.yaml"); + let config = Config::read_yaml_file("config.yaml"); let db = PgConnection::establish(&config.db_url) .expect("DB"); @@ -47,5 +47,5 @@ fn main() { .build() ); }); - gotham::start("[::1]:8400", router) + gotham::start("0.0.0.0:8400", router) } diff --git a/ticker-update/src/main.rs b/ticker-update/src/main.rs index 585f05d..b4baecd 100644 --- a/ticker-update/src/main.rs +++ b/ticker-update/src/main.rs @@ -258,7 +258,7 @@ impl std::fmt::Display for Error { } fn main() { - let config = Config::read_yaml_file("../config.yaml"); + let config = Config::read_yaml_file("config.yaml"); let res = Resources::new( config.db_url, config.calendars.into_iter()