summaryrefslogtreecommitdiff
path: root/nix/deployment.nix
blob: 88a9f51ef96b260cd068d270c1b2be15228981b4 (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
82
83
84
85
86
87
88
89
{
  inputs,
  flake-parts-lib,
  withSystem,
  ...
}: {
  flake.nixosModules.default = flake-parts-lib.importApply ./module.nix {
    inherit withSystem;
  };

  perSystem = {
    lib,
    pkgs,
    system,
    config,
    inputs',
    ...
  }: 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";
    };

    teawieFor = arch: let
      inherit (crossPkgs.${system}.${arch}.llvmPackages.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;
      };
    in
      (config.packages.teawiebot.override {
        inherit naersk;
        lto = true;
        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'}";
        });

    containerFor = arch:
      pkgs.dockerTools.buildLayeredImage {
        name = "teawiebot";
        tag = "latest-${arch}";
        contents = [pkgs.dockerTools.caCertificates];
        config.Cmd = [
          (lib.getExe (teawieFor arch))
        ];

        architecture = crossPkgs.${system}.${arch}.go.GOARCH;
      };
  in {
    packages = {
      teawiebot-static-x86_64 = teawieFor "x86_64";
      teawiebot-static-aarch64 = teawieFor "aarch64";
      container-x86_64 = containerFor "x86_64";
      container-aarch64 = containerFor "aarch64";
    };
  };
}