diff options
| author | seth <[email protected]> | 2023-04-07 02:16:25 -0400 |
|---|---|---|
| committer | seth <[email protected]> | 2023-04-07 03:42:48 -0400 |
| commit | 797297d1e6f1b9098aed473b2fc22b3236a525eb (patch) | |
| tree | 7c72c64792915c0ee4ff80fb5f0418104a742546 | |
| parent | 1e7dac94911d8210b678b2e76014ca3d07d8641b (diff) | |
refactor flake + minimize docker image
| -rw-r--r-- | flake.lock | 97 | ||||
| -rw-r--r-- | flake.nix | 185 |
2 files changed, 207 insertions, 75 deletions
@@ -1,5 +1,53 @@ { "nodes": { + "crane": { + "inputs": { + "flake-compat": [ + "flake-compat" + ], + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ], + "rust-overlay": "rust-overlay" + }, + "locked": { + "lastModified": 1680584903, + "narHash": "sha256-uraq+D3jcLzw/UVk0xMHcnfILfIMa0DLrtAEq2nNlxU=", + "owner": "ipetkov", + "repo": "crane", + "rev": "65d3f6a3970cd46bef5eedfd458300f72c56b3c5", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1680762089, + "narHash": "sha256-62lgi+xb+nn9H4O+ZIYNkHeQ8ryzstALKMJuoXiot0I=", + "owner": "nix-community", + "repo": "fenix", + "rev": "5794e58068fb6a8eccad9e4ff77ffe1c08ded13c", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, "flake-compat": { "flake": false, "locked": { @@ -70,7 +118,9 @@ }, "pre-commit-hooks": { "inputs": { - "flake-compat": "flake-compat", + "flake-compat": [ + "flake-compat" + ], "flake-utils": [ "flake-utils" ], @@ -98,10 +148,55 @@ }, "root": { "inputs": { + "crane": "crane", + "fenix": "fenix", + "flake-compat": "flake-compat", "flake-utils": "flake-utils", "nixpkgs": "nixpkgs", "pre-commit-hooks": "pre-commit-hooks" } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1680727375, + "narHash": "sha256-hb8AosuONAg0D9yoZ4VrBsjf5hINMYVLPEGekXF4qVE=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "ea22d245b671f97b820cf761108251c6292c3152", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": [ + "crane", + "flake-utils" + ], + "nixpkgs": [ + "crane", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1680488274, + "narHash": "sha256-0vYMrZDdokVmPQQXtFpnqA2wEgCCUXf5a3dDuDVshn0=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "7ec2ff598a172c6e8584457167575b3a1a5d80d8", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } } }, "root": "root", @@ -3,109 +3,146 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; + crane = { + url = "github:ipetkov/crane"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.flake-compat.follows = "flake-compat"; + inputs.flake-utils.follows = "flake-utils"; + }; + fenix = { + url = "github:nix-community/fenix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; pre-commit-hooks = { url = "github:cachix/pre-commit-hooks.nix"; inputs.nixpkgs.follows = "nixpkgs"; inputs.nixpkgs-stable.follows = "nixpkgs"; + inputs.flake-compat.follows = "flake-compat"; inputs.flake-utils.follows = "flake-utils"; }; - flake-utils.url = "github:numtide/flake-utils"; }; outputs = { self, nixpkgs, - pre-commit-hooks, flake-utils, + crane, + fenix, + pre-commit-hooks, ... }: let - version = "0.0.1"; supportedSystems = with flake-utils.lib.system; [ x86_64-linux x86_64-darwin aarch64-linux aarch64-darwin ]; + + packageFn = pkgs: let + inherit (pkgs.lib) licenses maintainers platforms; + craneLib = let + toolchain = with pkgs.fenix; + combine [ + stable.cargo + stable.rustc + targets."x86_64-unknown-linux-musl".stable.rust-std + ]; + in + (crane.mkLib pkgs).overrideToolchain toolchain; + inherit (craneLib) buildPackage; + cargoArtifacts = craneLib.buildDepsOnly { + src = ./.; + }; + in { + teawiebot = buildPackage { + src = ./.; + inherit cargoArtifacts; + + meta = { + description = "funni bot"; + homepage = "https://github.com/getchoo/teawiebot"; + license = licenses.mit; + platforms = platforms.unix; + maintainers = with maintainers; [getchoo]; + }; + }; + }; in flake-utils.lib.eachSystem supportedSystems (system: let - pkgs = import nixpkgs {inherit system;}; - in { - packages = let - inherit (pkgs.lib) maintainers licenses; - inherit (pkgs.dockerTools) buildLayeredImage caCertificates; - inherit (pkgs.rustPlatform) buildRustPackage; - in - rec { - teawiebot = buildRustPackage { - pname = "teawiebot"; - inherit version; + pkgs = import nixpkgs { + inherit system; + overlays = [fenix.overlays.default]; + }; + in { + packages = let + inherit (packageFn pkgs) teawiebot; + teawiebot-smol = + teawiebot.overrideAttrs (_: { + # statically link musl, optimize for size + CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; + CARGO_BUILD_RUSTFLAGS = "-C lto=fat -C embed-bitcode=yes \ + -C target-feature=+crt-static -C opt-level=z -C strip=symbols -C codegen-units=1"; + CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER = let + inherit (pkgs.pkgsStatic.stdenv) cc; + in "${cc}/bin/${cc.targetPrefix}cc"; + }); + in + { + inherit teawiebot; + container = let + inherit (pkgs.dockerTools) buildLayeredImage caCertificates; + cmd = "${teawiebot-smol}/bin/teawiebot"; + in + buildLayeredImage { + name = "teawiebot"; + tag = "latest"; + contents = [caCertificates]; + config.Cmd = ["${cmd}"]; + }; + } + // {default = self.packages.${system}.teawiebot;}; + checks = { + inherit (self.packages.${system}) teawiebot; + pre-commit-check = pre-commit-hooks.lib.${system}.run { src = ./.; - - RUSTFLAGS = "-C lto=thin -C embed-bitcode=yes"; - cargoSha256 = "sha256-TQThvhD2psA5+VGSMl3+dBOs8K33Fs5q42RovXnYYhY="; - - buildInputs = with pkgs; [ - openssl.dev - ]; - nativeBuildInputs = with pkgs; [ - pkg-config - ]; - - meta = { - description = "funni bot"; - homepage = "https://github.com/getchoo/teawiebot"; - license = licenses.mit; - maintainers = with maintainers; [getchoo]; + hooks = { + actionlint.enable = true; + alejandra.enable = true; + deadnix.enable = true; + statix.enable = true; }; }; - container = let - bot = teawiebot.overrideAttrs (prev: { - RUSTFLAGS = prev.RUSTFLAGS + " -C opt-level=s"; - }); - in - buildLayeredImage { - name = "teawiebot"; - tag = "latest"; - contents = [caCertificates]; - config.Cmd = ["${bot}/bin/teawiebot"]; - }; - } - // {default = self.packages.${system}.teawiebot;}; - - checks = { - pre-commit-check = pre-commit-hooks.lib.${system}.run { - src = ./.; - hooks = { - actionlint.enable = true; - alejandra.enable = true; - cargo-check.enable = true; - deadnix.enable = true; - rustfmt.enable = true; - statix.enable = true; - }; }; - }; - devShells = let - inherit (pkgs) mkShell; - in { - default = mkShell { - packages = with pkgs; [ - actionlint - alejandra - cargo - clippy - deadnix - openssl.dev - pkg-config - rustfmt - statix - ]; + devShells = let + inherit (pkgs) fenix mkShell; inherit (self.checks.${system}.pre-commit-check) shellHook; + in { + default = mkShell { + inherit shellHook; + packages = with pkgs; [ + actionlint + alejandra + clippy + deadnix + statix + (with fenix; + combine [ + stable.cargo + stable.rustc + stable.rustfmt + targets."x86_64-unknown-linux-musl".stable.rust-std + ]) + ]; + }; }; - }; - formatter = pkgs.alejandra; - }); + formatter = pkgs.alejandra; + }); } |
