From 0108e64c487347ba7c222c7148a810128a757f30 Mon Sep 17 00:00:00 2001 From: Philippe Loctaux Date: Tue, 19 Nov 2024 20:28:53 +0100 Subject: [PATCH] nix: initial packaging --- .gitignore | 4 + crates/plcom/tailwind.nix | 13 +++ flake.lock | 98 ++++++++++++++++++++++ flake.nix | 169 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 284 insertions(+) create mode 100644 crates/plcom/tailwind.nix create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.gitignore b/.gitignore index e54971b..46decf0 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ wallpapers.json # build output target/ pkg +/result # environment variables .env @@ -20,3 +21,6 @@ pkg # rustfmt **/*.rs.bk + +# vim +[._]*.sw[a-p] diff --git a/crates/plcom/tailwind.nix b/crates/plcom/tailwind.nix new file mode 100644 index 0000000..f3e399b --- /dev/null +++ b/crates/plcom/tailwind.nix @@ -0,0 +1,13 @@ +{ + pkgs, + src, + inputFile, +}: + +pkgs.stdenvNoCC.mkDerivation { + name = "plcom-css-tailwind"; + inherit src; + buildInputs = with pkgs; [ tailwindcss ]; + dontUnpack = true; + buildPhase = "tailwindcss --config ${src}/tailwind.config.js --input ${src}/${inputFile} --output $out/output.css --minify"; +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..847fa84 --- /dev/null +++ b/flake.lock @@ -0,0 +1,98 @@ +{ + "nodes": { + "crane": { + "locked": { + "lastModified": 1731974733, + "narHash": "sha256-enYSSZVVl15FI5p+0Y5/Ckf5DZAvXe6fBrHxyhA/njc=", + "owner": "ipetkov", + "repo": "crane", + "rev": "3cb338ce81076ce5e461cf77f7824476addb0e1c", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1731890469, + "narHash": "sha256-D1FNZ70NmQEwNxpSSdTXCSklBH1z2isPR84J6DQrJGs=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "5083ec887760adfe12af64830a66807423a859a7", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "crane": "crane", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1731983527, + "narHash": "sha256-JECaBgC0pQ91Hq3W4unH6K9to8s2Zl2sPNu7bLOv4ek=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "71287228d96e9568e1e70c6bbfa3f992d145947b", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..f1fbe95 --- /dev/null +++ b/flake.nix @@ -0,0 +1,169 @@ +{ + description = "philippeloctaux dot com"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + crane.url = "github:ipetkov/crane"; + flake-utils.url = "github:numtide/flake-utils"; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = + { + self, + nixpkgs, + crane, + flake-utils, + rust-overlay, + ... + }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ (import rust-overlay) ]; + }; + + inherit (pkgs) lib; + + tailwindStylesheet = import ./crates/plcom/tailwind.nix { + pkgs = import nixpkgs { inherit system; }; + src = ./crates/plcom; + inputFile = "css/main.css"; + }; + + plcomAssets = pkgs.stdenvNoCC.mkDerivation { + name = "plcom-assets"; + + # Local folder as a source + src = ./public; + + # Build inputs (external derivation dependencies) + buildInputs = [ tailwindStylesheet ]; + + installPhase = '' + mkdir -p $out + cp -r $src/* $out/ + cp ${tailwindStylesheet}/output.css $out/style.css + ''; + }; + + # in + craneLib = crane.mkLib pkgs; + + # Get metadata from Cargo.toml + metadata = craneLib.crateNameFromCargoToml { cargoToml = ./crates/plcom/Cargo.toml; }; + + # Common derivation arguments used for all builds + commonArgs = { + src = lib.cleanSourceWith { + src = craneLib.path ./.; # The original, unfiltered source + filter = + path: type: + # Assets for codegen + (lib.hasSuffix ".json" path) + || + # Static assets + # Default filter from crane (allow .rs files) + (craneLib.filterCargoSources path type); + }; + + strictDeps = true; + + buildInputs = + with pkgs; + [ + # Add additional build inputs here + openssl + ] + ++ lib.optionals pkgs.stdenv.isDarwin [ + pkgs.libiconv + ]; + + # nativeBuildInputs = with pkgs; [ + # # Add extra native build inputs here, etc. + # pkg-config + # ]; + + } // metadata; + + # Build *just* the cargo dependencies + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + + # Clippy + plcomClippy = craneLib.cargoClippy ( + commonArgs + // { + inherit cargoArtifacts; + # Again we apply some extra arguments only to this derivation + # and not every where else. In this case we add some clippy flags + # cargoClippyExtraArgs = "--all-targets -- --deny warnings"; + } + ); + + # Build crate + plcomBinary = craneLib.buildPackage ( + commonArgs + // { + cargoArtifacts = plcomClippy; + } + ); + + # How to launch binary + plcom = pkgs.writeShellScriptBin "plcom" '' + PLCOM_ASSETS_PATH=${plcomAssets} ${plcomBinary}/bin/plcom + ''; + + # Docker image + dockerImage = pkgs.dockerTools.buildLayeredImage { + # Meta + name = metadata.pname; + tag = metadata.version; + created = builtins.substring 0 8 self.lastModifiedDate; + + # Content of image + contents = pkgs.buildEnv { + name = "image-root"; + paths = [ + plcomBinary + plcomAssets + ]; + + pathsToLink = [ "/bin" ]; + }; + + # Container config + config = { + Cmd = [ "${plcomBinary}/bin/plcom" ]; + Env = [ + "PLCOM_ASSETS_PATH=${plcomAssets}" + ]; + }; + }; + in + { + apps.default = { + type = "app"; + program = "${plcom}/bin/plcom"; + }; + + packages = { + inherit plcom dockerImage; + default = plcom; + }; + + checks = { + inherit + # Build the crate as part of `nix flake check` for convenience + plcom + plcomClippy + ; + }; + + } + ); +}