From 0b7e1ae5aea05cdc5069b3edb15bd2165a2e1411 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Fri, 7 Oct 2022 11:56:36 -0700 Subject: ref(build): build and deploy with nix Refactored the build of the docker image to be done with nix: the flake knows how to build the docker image, using caddy as a HTTP server. It generates a small image, with the configuration for caddy and the site generated by hugo (`nix build`). Deleted the Dockerfile since the creation is done with nix. Got rid of the deployment script since this is also done via the flake (`nix run .#deploy`). --- flake.nix | 80 +++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 28 deletions(-) (limited to 'flake.nix') diff --git a/flake.nix b/flake.nix index 79e6953..a752a6e 100644 --- a/flake.nix +++ b/flake.nix @@ -8,39 +8,63 @@ outputs = { self, nixpkgs, flake-utils }: flake-utils.lib.eachDefaultSystem (system: - let pkgs = nixpkgs.legacyPackages.${system}; - in { - defaultPackage = with pkgs; - stdenv.mkDerivation { - pname = "fcuny.net"; - version = self.lastModifiedDate; - src = ./.; - buildInputs = [ hugo git ]; - buildPhase = '' - mkdir -p $out - hugo --minify --destination $out - ''; - dontInstall = true; + let + pkgs = nixpkgs.legacyPackages.${system}; + caddyfile = ./Caddyfile; + in + { + packages = { + site = with pkgs; + stdenv.mkDerivation { + pname = "fcuny.net"; + version = self.lastModifiedDate; + src = ./.; + buildInputs = [ hugo git ]; + buildPhase = '' + mkdir -p $out + hugo --minify --destination $out + ''; + dontInstall = true; + }; + container = pkgs.dockerTools.buildLayeredImage { + name = self.packages."${system}".site.pname; + tag = self.packages."${system}".site.version; + config = { + Cmd = [ "${pkgs.caddy}/bin/caddy" "run" "--adapter" "caddyfile" "--config" "${caddyfile}" ]; + Env = [ + "SITE_ROOT=${self.packages."${system}".site}" + ]; + }; }; - - defaultApp = pkgs.writers.writeBashBin "run-hugo" '' - set -e - set -o pipefail - export PATH=${pkgs.lib.makeBinPath [ pkgs.hugo pkgs.git ]} - hugo server -D - ''; - - apps = { - deploy = pkgs.pkgs.writeShellScriptBin "run-deploy" '' + deploy = pkgs.writeShellScriptBin "deploy" '' set -euxo pipefail - export PATH=${ - pkgs.lib.makeBinPath [ pkgs.hugo pkgs.git pkgs.jq pkgs.flyctl ] - }:$PATH - bash ./scripts/deploy.sh + export PATH="${pkgs.lib.makeBinPath [(pkgs.docker.override { clientOnly = true; }) pkgs.flyctl]}:$PATH" + archive=${self.packages.x86_64-linux.container} + # load archive, drop all output except last line case of warnings), print image name + image=$(docker load < $archive | tail -n1 | awk '{ print $3; }') + flyctl deploy --image $image --local-only ''; + hugo = pkgs.writeShellScriptBin "hugo" '' + set -euo pipefail + export PATH=${pkgs.lib.makeBinPath [ pkgs.hugo pkgs.git ]} + hugo server -D + ''; + }; + + apps = { + deploy = { + type = "app"; + program = "${self.packages."${system}".deploy}/bin/deploy"; + }; + default = { + type = "app"; + program = "${self.packages."${system}".hugo}/bin/hugo"; + }; }; + defaultPackage = self.packages."${system}".container; + devShell = - pkgs.mkShell { buildInputs = with pkgs; [ hugo flyctl git jq ]; }; + pkgs.mkShell { buildInputs = with pkgs; [ hugo flyctl git ]; }; }); } -- cgit 1.4.1