refactor how nix derivations are organized and built

pull/15/head
Brian Picciano 3 years ago
parent 0197d9cd49
commit 6feffc568a
  1. 25
      Makefile
  2. 8
      config.nix
  3. 170
      default.nix
  4. 13
      nix/default.nix
  5. 7
      srv/Makefile
  6. 52
      srv/default.nix
  7. 18
      static/Makefile
  8. 82
      static/default.nix

@ -1,4 +1,25 @@
install: all:
nix-build -A install 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 ./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"; powSecret = "ssshhh";
mlSMTPAddr = ""; mlSMTPAddr = "";
mlSMTPAuth = ""; mlSMTPAuth = "";
dataDir = "/tmp/mediocre-blog-data";
publicURL = "http://localhost:4000"; publicURL = "http://localhost:4000";
listenProto = "tcp"; listenProto = "tcp";
listenAddr = ":4000"; listenAddr = ":4000";
redisListenPath = "/tmp/mediocre-blog-redis";
# If empty then a derived static directory is used
staticProxyURL = "http://127.0.0.1:4001";
} }

@ -1,84 +1,86 @@
let {
utils = (import ./nix) {};
pkgs = utils.pkgs; pkgs ? import (fetchTarball {
system = utils.system; name = "nixpkgs-21-05";
in url = "https://github.com/NixOS/nixpkgs/archive/7e9b0dff974c89e070da1ad85713ff3c20b0ca97.tar.gz";
{config ? ./config.nix}: rec { sha256 = "1ckzhh24mgz6jd1xhfgx0i9mijk6xjqxwsshnvq789xsavrmsc36";
config = (import ./config.nix); }) {},
static = (import ./static).build; baseConfig ? import ./config.nix,
srv = (import ./srv).build; }: rec {
srvBin = pkgs.writeScript "mediocregopher-mediocre-blog-srvBin" ''
#!/bin/sh config = baseConfig // {
exec ${srv}/bin/mediocre-blog \ redisListenPath = "${config.runDir}/redis";
-pow-secret "${config.powSecret}" \ };
-ml-smtp-addr "${config.mlSMTPAddr}" \
-ml-smtp-auth "${config.mlSMTPAuth}" \ static = (import ./static) { inherit pkgs; };
-data-dir "${config.dataDir}" \
-public-url "${config.publicURL}" \ srv = (import ./srv) {
-static-dir "${static}" \ inherit pkgs config;
-listen-proto "${config.listenProto}" \ staticBuild=static.build;
-listen-addr "${config.listenAddr}" };
'';
redisCfg = pkgs.writeText "mediocre-blog-redisCfg" ''
redisCfg = pkgs.writeText "mediocregopher-mediocre-blog-redisCfg" '' port 0
port 0 unixsocket ${config.redisListenPath}
unixsocket ${config.redisListenPath} daemonize no
daemonize no loglevel notice
loglevel notice logfile ""
logfile "" appendonly yes
appendonly yes appendfilename "appendonly.aof"
appendfilename "appendonly.aof" dir ${config.dataDir}/redis
dir ${config.dataDir}/redis '';
'';
redisBin = pkgs.writeScript "mediocre-blog-redisBin" ''
redisBin = pkgs.writeScript "mediocregopher-mediocre-blog-redisBin" '' #!/bin/sh
#!/bin/sh mkdir -p ${config.dataDir}/redis
mkdir -p ${config.dataDir}/redis exec ${pkgs.redis}/bin/redis-server ${redisCfg}
exec ${pkgs.redis}/bin/redis-server ${redisCfg} '';
'';
circusCfg = pkgs.writeText "mediocre-blog-circusCfg" ''
circusCfg = pkgs.writeText "mediocregopher-mediocre-blog-circusCfg" '' [circus]
[circus] endpoint = tcp://127.0.0.1:0
endpoint = tcp://127.0.0.1:0 pubsub_endpoint = tcp://127.0.0.1:0
pubsub_endpoint = tcp://127.0.0.1:0
[watcher:srv]
[watcher:srv] cmd = ${srv.bin}
cmd = ${srvBin} numprocesses = 1
numprocesses = 1
[watcher:redis]
[watcher:redis] cmd = ${redisBin}
cmd = ${redisBin} numprocesses = 1
numprocesses = 1 '';
'';
entrypoint = pkgs.writeScript "mediocre-blog-entrypoint" ''
circusBin = pkgs.writeScript "mediocregopher-mediocre-blog-circusBin" '' #!/bin/sh
exec ${pkgs.circus}/bin/circusd ${circusCfg} mkdir -p ${config.runDir}
''; mkdir -p ${config.dataDir}
exec ${pkgs.circus}/bin/circusd ${circusCfg}
service = pkgs.writeText "mediocregopher-mediocre-blog" '' '';
[Unit]
Description=mediocregopher mediocre blog service = pkgs.writeText "mediocre-blog" ''
Requires=network.target [Unit]
After=network.target Description=mediocregopher mediocre blog
Requires=network.target
[Service] After=network.target
Restart=always
RestartSec=1s [Service]
User=mediocregopher Restart=always
ExecStart=${circusBin} RestartSec=1s
User=mediocregopher
[Install] ExecStart=${entrypoint}
WantedBy=multi-user.target
''; [Install]
WantedBy=multi-user.target
install = pkgs.writeScript "mediocregopher-mediocre-blog" '' '';
set -e -x
install = pkgs.writeScript "mediocre-blog" ''
sudo cp ${service} /etc/systemd/system/mediocregopher-mediocre-blog.service set -e -x
sudo systemctl daemon-reload
sudo systemctl enable mediocregopher-mediocre-blog.service sudo cp ${service} /etc/systemd/system/mediocregopher-mediocre-blog.service
sudo systemctl restart mediocregopher-mediocre-blog.service sudo systemctl daemon-reload
''; sudo systemctl enable mediocregopher-mediocre-blog.service
} sudo systemctl restart mediocregopher-mediocre-blog.service
'';
}

@ -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;
}

@ -1,7 +0,0 @@
build:
nix-build -A build
shell:
nix-shell -A shell

@ -1,20 +1,38 @@
let {pkgs, config, staticBuild}: rec {
utils = (import ../nix) {};
pkgs = utils.pkgs;
system = utils.system;
in
{
build = pkgs.buildGoModule { opts = [
pname = "mediocre-blog-srv"; "-pow-secret=${config.powSecret}"
version = "dev"; "-ml-smtp-addr=${config.mlSMTPAddr}"
src = ./.; "-ml-smtp-auth='${config.mlSMTPAuth}'"
vendorSha256 = "08wv94yv2wmlxzmanw551gixc8v8nl6zq2m721ig9nl3r540x46f"; "-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 { build = pkgs.buildGoModule {
name = "mediocre-blog-srv-shell"; pname = "mediocre-blog-srv";
buildInputs = [ pkgs.go ]; 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 ];
};
}

@ -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'

@ -1,53 +1,39 @@
let {pkgs}: rec {
utils = (import ../nix) {};
pkgs = utils.pkgs;
system = utils.system;
jekyll_env = pkgs.bundlerEnv { depInputs = [ pkgs.imagemagick pkgs.exiftool pkgs.bundler pkgs.bundix ];
name = "jekyll_env";
depShell = pkgs.stdenv.mkDerivation {
name = "mediocre-blog-static-dep-shell";
buildInputs = depInputs;
};
jekyllEnv = pkgs.bundlerEnv {
name = "jekyllEnv";
ruby = pkgs.ruby; ruby = pkgs.ruby;
gemdir = ./.; gemdir = ./.;
}; };
dep_inputs = [ pkgs.imagemagick pkgs.exiftool pkgs.bundler pkgs.bundix ];
all_inputs = [ jekyll_env ] ++ dep_inputs; build = pkgs.stdenv.mkDerivation {
in name = "mediocre-blog-static";
{ src = ./src;
build = derivation { buildPhase = "${jekyllEnv}/bin/jekyll build";
inherit jekyll_env system; installPhase = "mv _site $out";
};
name = "mediocre-blog-static";
builder = "${pkgs.bash}/bin/bash"; serve = pkgs.writeScriptBin "static-serve" ''
args = [ #!/bin/sh
(pkgs.writeTextFile { exec ${jekyllEnv}/bin/jekyll serve \
name = "mediocre-blog-static-buildsh"; -s ./src \
text = '' -d ./_site \
source ${pkgs.stdenv}/setup -w -I -D \
set -e -P 4001
'';
mkdir -p "$out"
$jekyll_env/bin/jekyll build -s "${./src}" -d "$out" allInputs = depInputs ++ [ jekyllEnv serve ];
'';
executable = true; shell = pkgs.stdenv.mkDerivation {
}) name = "mediocre-blog-static-shell";
]; buildInputs = allInputs;
}; };
}
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;
};
}

Loading…
Cancel
Save