about summary refs log tree commit diff
path: root/flake.nix
blob: 25444f8aa6be3fe1f97ae018952cf7eab5b099d4 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{
  description = "sild - The Small Interactive Lua Debugger";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

    # Fenix provides the rust toolchains
    fenix.url = "github:nix-community/fenix";
    fenix.inputs.nixpkgs.follows = "nixpkgs";

    # Naersk builds the rust project
    naersk.url = "github:nix-community/naersk";
    naersk.inputs.nixpkgs.follows = "nixpkgs";

    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs =
    {
      self,
      nixpkgs,
      fenix,
      naersk,
      flake-utils,
    }:
    let
      # TODO: Add x86_64-darwin and aarch64-darwin
      buildTargets = {
        "x86_64-linux" = {
          crossSystemConfig = "x86_64-unknown-linux-musl";
          rustTarget = "x86_64-unknown-linux-musl";
        };
        "aarch64-linux" = {
          crossSystemConfig = "aarch64-unknown-linux-musl";
          rustTarget = "aarch64-unknown-linux-musl";
        };
      };

      makePackage =
        buildSystem: targetName:
        let
          target = buildTargets.${targetName};
          rustTarget = target.rustTarget;

          pkgs = import nixpkgs {
            system = buildSystem;
            overlays = [ fenix.overlays.default ];
          };

          pkgsCross = import nixpkgs {
            system = buildSystem;
            crossSystem = {
              config = target.crossSystemConfig;
              isStatic = true;
            };
            overlays = [ fenix.overlays.default ];
          };

          fenixPkgs = pkgs.fenix;

          toolchain = fenixPkgs.combine [
            fenixPkgs.stable.rustc
            fenixPkgs.stable.cargo
            fenixPkgs.targets.${rustTarget}.stable.rust-std
          ];

          naersk-lib = pkgs.callPackage naersk {
            cargo = toolchain;
            rustc = toolchain;
          };

          targetCC = "${pkgsCross.stdenv.cc}/bin/${pkgsCross.stdenv.cc.targetPrefix}cc";

        in
        naersk-lib.buildPackage {
          name = "sild-${targetName}";
          src = ./.;

          strictDeps = true;
          doCheck = false;

          # Don't strip Lua
          singleStep = true;

          TARGET_CC = targetCC;
          "CC_${builtins.replaceStrings [ "-" ] [ "_" ] rustTarget}" = targetCC;

          CARGO_BUILD_TARGET = rustTarget;
          CARGO_BUILD_RUSTFLAGS = [
            "-C"
            "target-feature=+crt-static"
            "-C"
            "link-args=-static"
            "-C"
            "linker=${targetCC}"
          ];
        };

      perSystemOutputs =
        buildSystem:
        let
          pkgs = import nixpkgs {
            system = buildSystem;
            overlays = [ fenix.overlays.default ];
          };

          devToolchain = pkgs.fenix.stable.withComponents [
            "rustc"
            "cargo"
            "rust-src"
            "clippy"
            "rustfmt"
            "rust-std"
          ];

        in
        {
          packages =
            builtins.mapAttrs (targetName: _: makePackage buildSystem targetName) buildTargets
            //
              (
                let
                  nativeTarget =
                    if pkgs.stdenv.isLinux then
                      "${builtins.head (builtins.match "([^-]+)-.*" buildSystem)}-linux"
                    else
                      null;
                in
                if nativeTarget != null && buildTargets ? ${nativeTarget} then
                  { default = makePackage buildSystem nativeTarget; }
                else
                  { }
              );

          devShells.default = pkgs.mkShell {
            name = "sild-dev";

            nativeBuildInputs = [
              devToolchain
              pkgs.rust-analyzer
              pkgs.pkg-config

              # Cross linkers for musl
              pkgs.pkgsCross.aarch64-multiplatform-musl.stdenv.cc
              pkgs.pkgsCross.musl64.stdenv.cc
            ];

            # Musl cross linkers locations
            CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER = "${pkgs.pkgsCross.musl64.stdenv.cc}/bin/${pkgs.pkgsCross.musl64.stdenv.cc.targetPrefix}cc";
            CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER = "${pkgs.pkgsCross.aarch64-multiplatform-musl.stdenv.cc}/bin/${pkgs.pkgsCross.aarch64-multiplatform-musl.stdenv.cc.targetPrefix}cc";

            shellHook = ''
              echo ""
              echo "  SILD Nix Devenv"
              echo "  Rust: v$(rustc --version |  awk '{print $2}')"
              echo "  mlua: $(cargo tree | grep 'mlua v' | awk '{print $3}')"
              echo ""
              echo "  Build commands:"
              echo "    cargo build --release                                     # native debug build"
              echo "    nix build .#x86_64-linux                                  # static x86_64 build"
              echo "    nix build .#aarch64-linux                                 # static aarch64 build"
              echo ""
            '';
          };
        };

    in
    flake-utils.lib.eachDefaultSystem (system: perSystemOutputs system);
}