summaryrefslogtreecommitdiff
path: root/nix/deployment.nix
blob: 080cac9778cd1f3f2a3f5f4a4e49cbfd93f05600 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{
  inputs,
  flake-parts-lib,
  withSystem,
  ...
}: {
  flake.nixosModules.default = flake-parts-lib.importApply ./module.nix {
    inherit withSystem;
  };

  perSystem = {
    lib,
    pkgs,
    system,
    config,
    inputs',
    ...
  }: let
    containerFor = arch: let
      crossPkgs = {
        "x86_64-linux" = {
          "x86_64" = pkgs.pkgsStatic;
          "aarch64" = pkgs.pkgsCross.aarch64-multiplatform.pkgsStatic;
        };

        "aarch64-linux" = {
          "x86_64" = pkgs.pkgsCross.musl64;
          "aarch64" = pkgs.pkgsStatic;
        };

        "x86_64-darwin" = {
          "x86_64" = pkgs.pkgsCross.musl64;
          "aarch64" = pkgs.pkgsCross.aarch64-multiplatform.pkgsStatic;
        };

        "aarch64-darwin" = crossPkgs."x86_64-darwin";
      };
      inherit (crossPkgs.${system}.${arch}.stdenv) cc;

      target = "${arch}-unknown-linux-musl";
      target' = builtins.replaceStrings ["-"] ["_"] target;
      targetUpper = lib.toUpper target';

      toolchain = with inputs'.fenix.packages;
        combine [
          minimal.cargo
          minimal.rustc
          targets.${target}.latest.rust-std
        ];

      naersk = inputs.naersk.lib.${system}.override {
        cargo = toolchain;
        rustc = toolchain;
      };

      teawiebot =
        (config.packages.teawiebot.override {
          inherit naersk;
          optimizeSize = true;
        })
        .overrideAttrs (new:
          lib.const {
            CARGO_BUILD_TARGET = target;
            "CC_${target'}" = "${cc}/bin/${cc.targetPrefix}cc";
            "CARGO_TARGET_${targetUpper}_RUSTFLAGS" = "-C target-feature=+crt-static";
            "CARGO_TARGET_${targetUpper}_LINKER" = new."CC_${target'}";
          });
    in
      pkgs.dockerTools.buildLayeredImage {
        name = "teawiebot";
        tag = "latest-${arch}";
        contents = [pkgs.dockerTools.caCertificates];
        config.Cmd = [(lib.getExe teawiebot)];
      };
  in {
    packages = {
      container-x86_64 = containerFor "x86_64";
      container-aarch64 = containerFor "aarch64";
    };
  };
}