summaryrefslogtreecommitdiff
path: root/default.nix
blob: 4352e5cc37e1603244b8bb1a80ca2815733194a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
{

    pkgs ? import (fetchTarball {
        name = "nixpkgs-21-05";
        url = "https://github.com/NixOS/nixpkgs/archive/7e9b0dff974c89e070da1ad85713ff3c20b0ca97.tar.gz";
        sha256 = "1ckzhh24mgz6jd1xhfgx0i9mijk6xjqxwsshnvq789xsavrmsc36";
    }) {},

    baseConfig ? import ./config.nix,

}: rec {

    config = baseConfig // {
        redisListenPath = "${config.runDir}/redis";
    };

    static = (import ./static) { inherit pkgs; };

    srv = (import ./srv) {
        inherit pkgs config;
        staticBuild=static.build;
    };

    redisCfg = pkgs.writeText "mediocre-blog-redisCfg" ''
        port 0
        unixsocket ${config.redisListenPath}
        daemonize no
        loglevel notice
        logfile ""
        appendonly yes
        appendfilename "appendonly.aof"
        dir ${config.dataDir}/redis
    '';

    redisBin = pkgs.writeScript "mediocre-blog-redisBin" ''
        #!/bin/sh
        mkdir -p ${config.dataDir}/redis
        exec ${pkgs.redis}/bin/redis-server ${redisCfg}
    '';

    circusCfg = pkgs.writeText "mediocre-blog-circusCfg" ''
        [circus]
        endpoint = tcp://127.0.0.1:0
        pubsub_endpoint = tcp://127.0.0.1:0

        [watcher:srv]
        cmd = ${srv.bin}
        numprocesses = 1

        [watcher:redis]
        cmd = ${redisBin}
        numprocesses = 1
    '';

    entrypoint = pkgs.writeScript "mediocre-blog-entrypoint" ''
        #!/bin/sh
        mkdir -p ${config.runDir}
        mkdir -p ${config.dataDir}
        exec ${pkgs.circus}/bin/circusd ${circusCfg}
    '';

    service = pkgs.writeText "mediocre-blog" ''
        [Unit]
        Description=mediocregopher mediocre blog
        Requires=network.target
        After=network.target

        [Service]
        Restart=always
        RestartSec=1s
        User=mediocregopher
        ExecStart=${entrypoint}

        [Install]
        WantedBy=multi-user.target
    '';

    install = pkgs.writeScript "mediocre-blog" ''
        set -e -x

        sudo cp ${service} /etc/systemd/system/mediocregopher-mediocre-blog.service
        sudo systemctl daemon-reload
        sudo systemctl enable mediocregopher-mediocre-blog.service
        sudo systemctl restart mediocregopher-mediocre-blog.service
    '';
}