diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2021-08-08 07:57:45 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2021-08-08 08:43:17 -0600 |
commit | 6feffc568ae790a883b9e0509d755e7d9bd82ee1 (patch) | |
tree | f913ce47e9f8204e5cf882f382d10e178e02367f | |
parent | 0197d9cd493b5785bca05f476856540ec64da64a (diff) |
refactor how nix derivations are organized and built
-rw-r--r-- | Makefile | 25 | ||||
-rw-r--r-- | config.nix | 8 | ||||
-rw-r--r-- | default.nix | 170 | ||||
-rw-r--r-- | nix/default.nix | 13 | ||||
-rw-r--r-- | srv/Makefile | 7 | ||||
-rw-r--r-- | srv/default.nix | 52 | ||||
-rw-r--r-- | static/Makefile | 18 | ||||
-rw-r--r-- | static/default.nix | 82 |
8 files changed, 184 insertions, 191 deletions
@@ -1,4 +1,25 @@ -install: - nix-build -A install +all: + nix-build -A entrypoint --arg baseConfig '(import ./config.nix) // { staticProxyURL = ""; }' + +all.prod: + nix-build -A entrypoint --arg baseConfig '(import ./prod.config.nix)' + +install.prod: + nix-build -A install --arg baseConfig '(import ./prod.config.nix)' ./result + +srv.shell: + nix-shell -A srv.shell --command 'cd srv; return' + +static.shell: + nix-shell -A static.shell --command 'cd static; return' + +static.serve: + nix-shell -A static.shell --run 'cd static; static-serve' + +static.depShell: + nix-shell -A static.depShell --command 'cd static; return' + +static.lock: + nix-shell -A static.depShell --run 'bundler lock; bundix; rm -rf .bundle vendor' @@ -1,10 +1,14 @@ { + runDir = "/tmp/mediocre-blog/run"; + dataDir = "/tmp/mediocre-blog/data"; + powSecret = "ssshhh"; mlSMTPAddr = ""; mlSMTPAuth = ""; - dataDir = "/tmp/mediocre-blog-data"; publicURL = "http://localhost:4000"; listenProto = "tcp"; listenAddr = ":4000"; - redisListenPath = "/tmp/mediocre-blog-redis"; + + # If empty then a derived static directory is used + staticProxyURL = "http://127.0.0.1:4001"; } diff --git a/default.nix b/default.nix index a713f8a..4352e5c 100644 --- a/default.nix +++ b/default.nix @@ -1,84 +1,86 @@ -let - utils = (import ./nix) {}; - pkgs = utils.pkgs; - system = utils.system; -in - {config ? ./config.nix}: rec { - config = (import ./config.nix); - - static = (import ./static).build; - - srv = (import ./srv).build; - srvBin = pkgs.writeScript "mediocregopher-mediocre-blog-srvBin" '' - #!/bin/sh - exec ${srv}/bin/mediocre-blog \ - -pow-secret "${config.powSecret}" \ - -ml-smtp-addr "${config.mlSMTPAddr}" \ - -ml-smtp-auth "${config.mlSMTPAuth}" \ - -data-dir "${config.dataDir}" \ - -public-url "${config.publicURL}" \ - -static-dir "${static}" \ - -listen-proto "${config.listenProto}" \ - -listen-addr "${config.listenAddr}" - ''; - - redisCfg = pkgs.writeText "mediocregopher-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 "mediocregopher-mediocre-blog-redisBin" '' - #!/bin/sh - mkdir -p ${config.dataDir}/redis - exec ${pkgs.redis}/bin/redis-server ${redisCfg} - ''; - - circusCfg = pkgs.writeText "mediocregopher-mediocre-blog-circusCfg" '' - [circus] - endpoint = tcp://127.0.0.1:0 - pubsub_endpoint = tcp://127.0.0.1:0 - - [watcher:srv] - cmd = ${srvBin} - numprocesses = 1 - - [watcher:redis] - cmd = ${redisBin} - numprocesses = 1 - ''; - - circusBin = pkgs.writeScript "mediocregopher-mediocre-blog-circusBin" '' - exec ${pkgs.circus}/bin/circusd ${circusCfg} - ''; - - service = pkgs.writeText "mediocregopher-mediocre-blog" '' - [Unit] - Description=mediocregopher mediocre blog - Requires=network.target - After=network.target - - [Service] - Restart=always - RestartSec=1s - User=mediocregopher - ExecStart=${circusBin} - - [Install] - WantedBy=multi-user.target - ''; - - install = pkgs.writeScript "mediocregopher-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 - ''; - } +{ + + 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 + ''; +} diff --git a/nix/default.nix b/nix/default.nix deleted file mode 100644 index 438e083..0000000 --- a/nix/default.nix +++ /dev/null @@ -1,13 +0,0 @@ -{ - pkgs ? import (fetchTarball { - name = "nixpkgs-21-05"; - url = "https://github.com/NixOS/nixpkgs/archive/7e9b0dff974c89e070da1ad85713ff3c20b0ca97.tar.gz"; - sha256 = "1ckzhh24mgz6jd1xhfgx0i9mijk6xjqxwsshnvq789xsavrmsc36"; - }) {}, - system ? builtins.currentSystem, -}: - { - pkgs = pkgs; - system = system; - } - diff --git a/srv/Makefile b/srv/Makefile deleted file mode 100644 index 173bf6a..0000000 --- a/srv/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -build: - nix-build -A build - -shell: - nix-shell -A shell - - diff --git a/srv/default.nix b/srv/default.nix index e4babab..8a12f33 100644 --- a/srv/default.nix +++ b/srv/default.nix @@ -1,20 +1,38 @@ -let - utils = (import ../nix) {}; - pkgs = utils.pkgs; - system = utils.system; -in - { +{pkgs, config, staticBuild}: rec { - build = pkgs.buildGoModule { - pname = "mediocre-blog-srv"; - version = "dev"; - src = ./.; - vendorSha256 = "08wv94yv2wmlxzmanw551gixc8v8nl6zq2m721ig9nl3r540x46f"; - }; + opts = [ + "-pow-secret=${config.powSecret}" + "-ml-smtp-addr=${config.mlSMTPAddr}" + "-ml-smtp-auth='${config.mlSMTPAuth}'" + "-data-dir=${config.dataDir}" + "-public-url=${config.publicURL}" + "-listen-proto=${config.listenProto}" + "-listen-addr=${config.listenAddr}" + ] ++ ( + if config.staticProxyURL == "" + then [ "-static-dir=${staticBuild}" ] + else [ "-static-proxy-url=${config.staticProxyURL}" ] + ); - shell = pkgs.stdenv.mkDerivation { - name = "mediocre-blog-srv-shell"; - buildInputs = [ pkgs.go ]; - }; + build = pkgs.buildGoModule { + pname = "mediocre-blog-srv"; + version = "dev"; + src = ./.; + vendorSha256 = "08wv94yv2wmlxzmanw551gixc8v8nl6zq2m721ig9nl3r540x46f"; + }; - } + bin = pkgs.writeScript "mediocre-blog-srv-bin" '' + #!/bin/sh + exec ${build}/bin/mediocre-blog ${toString opts} + ''; + + runScript = pkgs.writeScriptBin "run-mediocre-blog" '' + go run ./cmd/mediocre-blog/main.go ${toString opts} + ''; + + shell = pkgs.stdenv.mkDerivation { + name = "mediocre-blog-srv-shell"; + buildInputs = [ pkgs.go runScript ]; + }; + +} diff --git a/static/Makefile b/static/Makefile deleted file mode 100644 index 1956a35..0000000 --- a/static/Makefile +++ /dev/null @@ -1,18 +0,0 @@ -build: - nix-build -A build - -clean: - rm -f result - rm -rf _site - -dev: - nix-shell -A dev - -shell: - nix-shell -A shell - -lock: - nix-shell -A depShell --run 'bundler lock; bundix; rm -rf .bundle vendor' - -update: - nix-shell -A depShell --run 'bundler update; bundler lock; bundix; rm -rf .bundle vendor' diff --git a/static/default.nix b/static/default.nix index 74bd3bf..7c50dfa 100644 --- a/static/default.nix +++ b/static/default.nix @@ -1,53 +1,39 @@ -let - utils = (import ../nix) {}; - pkgs = utils.pkgs; - system = utils.system; +{pkgs}: rec { - jekyll_env = pkgs.bundlerEnv { - name = "jekyll_env"; + depInputs = [ pkgs.imagemagick pkgs.exiftool pkgs.bundler pkgs.bundix ]; + + depShell = pkgs.stdenv.mkDerivation { + name = "mediocre-blog-static-dep-shell"; + buildInputs = depInputs; + }; + + jekyllEnv = pkgs.bundlerEnv { + name = "jekyllEnv"; ruby = pkgs.ruby; gemdir = ./.; }; - dep_inputs = [ pkgs.imagemagick pkgs.exiftool pkgs.bundler pkgs.bundix ]; - all_inputs = [ jekyll_env ] ++ dep_inputs; -in - { - build = derivation { - inherit jekyll_env system; - - name = "mediocre-blog-static"; - builder = "${pkgs.bash}/bin/bash"; - args = [ - (pkgs.writeTextFile { - name = "mediocre-blog-static-buildsh"; - text = '' - source ${pkgs.stdenv}/setup - set -e - - mkdir -p "$out" - $jekyll_env/bin/jekyll build -s "${./src}" -d "$out" - ''; - executable = true; - }) - ]; - }; - - dev = pkgs.stdenv.mkDerivation { - name = "mediocre-blog-static-dev"; - buildInputs = all_inputs; - shellHook = '' - exec ${jekyll_env}/bin/jekyll serve -s ./src -d ./_site -w -I -D -H 0.0.0.0 -P 4001 - ''; - }; - - depShell = pkgs.stdenv.mkDerivation { - name = "mediocre-blog-static-dep-shell"; - buildInputs = dep_inputs; - }; - - shell = pkgs.stdenv.mkDerivation { - name = "mediocre-blog-static-shell"; - buildInputs = all_inputs; - }; - } + + build = pkgs.stdenv.mkDerivation { + name = "mediocre-blog-static"; + src = ./src; + buildPhase = "${jekyllEnv}/bin/jekyll build"; + installPhase = "mv _site $out"; + }; + + serve = pkgs.writeScriptBin "static-serve" '' + #!/bin/sh + exec ${jekyllEnv}/bin/jekyll serve \ + -s ./src \ + -d ./_site \ + -w -I -D \ + -P 4001 + ''; + + allInputs = depInputs ++ [ jekyllEnv serve ]; + + shell = pkgs.stdenv.mkDerivation { + name = "mediocre-blog-static-shell"; + buildInputs = allInputs; + }; +} |