aboutsummaryrefslogtreecommitdiff
path: root/flake.nix
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2022-10-07 11:56:36 -0700
committerFranck Cuny <franck@fcuny.net>2022-10-07 11:56:36 -0700
commit0b7e1ae5aea05cdc5069b3edb15bd2165a2e1411 (patch)
treed883ca82a5a6a3361f36549eb703a19d9d9d4fa0 /flake.nix
parentbuild: drop the configuration for drone (diff)
downloadfcuny.net-0b7e1ae5aea05cdc5069b3edb15bd2165a2e1411.tar.gz
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`).
Diffstat (limited to 'flake.nix')
-rw-r--r--flake.nix80
1 files changed, 52 insertions, 28 deletions
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 ]; };
});
}