summaryrefslogtreecommitdiff
path: root/flake.nix
blob: d89c6d36a5906d0ef1d89c62e134f891d38e8b5a (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
{
  description = "teawie moment";

  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";
    };
  };

  outputs = {
    self,
    nixpkgs,
    flake-utils,
    crane,
    fenix,
    pre-commit-hooks,
    ...
  }: let
    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;
          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 = ./.;
            hooks = {
              actionlint.enable = true;
              alejandra.enable = true;
              deadnix.enable = true;
              statix.enable = true;
            };
          };
        };

        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;
      });
}