aboutsummaryrefslogtreecommitdiffstats
path: root/flake.nix
blob: e5990bf8b036458dbce434ffa0bae28f112e68e1 (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
{
  description = "A very basic flake";

  inputs.nixpkgs = {
    type = "github";
    owner = "nixos";
    repo = "nixpkgs";
    ref = "master";
  };

  outputs = inputs@{ self, nixpkgs }: let
    systems = [ "x86_64-linux" "aarch64-linux" "i686-linux" "x86_64-darwin" ];

    forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system);

    nixpkgsFor = forAllSystems (system:
      import nixpkgs {
        inherit system;
        overlays = [ self.overlay ];
      }
    );
  in {

    overlay = final: prev: {
      vendorCrates = with final; let
        lockFile = builtins.fromTOML (builtins.readFile ./Cargo.lock);#(builtins.readFile self + "./Cargo.lock");

        files = map (pkg: fetchurl {
          name = pkg.name;
          url = "https://crates.io/api/v1/crates/${pkg.name}/${pkg.version}/download";
          sha256 = pkg.checksum;
        }) (builtins.filter (pkg: pkg.source or "" == "registry+https://github.com/rust-lang/crates.io-index") lockFile.package);
      in runCommand "cargo-vendor-dir" {}
        ''
          mkdir -p $out/vendor

          cat > $out/vendor/config << EOF
          [source.crates-io]
          replace-with = "vendored-sources"

          [source.vendored-sources]
          directory = "vendor"
          EOF
          ${toString (builtins.map (file: ''
            mkdir $out/vendor/tmp
            tar xvf ${file} -C $out/vendor/tmp
            dir=$(echo $out/vendor/tmp/*)

            printf '{"files":{},"package":"${file.outputHash}"}' > "$dir/.cargo-checksum.json"

            if [[ $dir =~ /winapi ]]; then
              find $dir -name "*.a" -print0 | xargs -0 rm -f --
            fi

            mv "$dir" $out/vendor/

            rm -rf $out/vendor/tmp
          '') files)}
        '';
      backend = with final; stdenv.mkDerivation {
        name = "backend";

        src = self;

        nativeBuildInputs = [
          cargo rustc
        ];

        buildPhase = ''
          ln -sfn ${vendorCrates}/vendor/ vendor
          export CARGO_HOME=$(pwd)/vendor
          cargo build --release --offline
        '';

        installPhase = ''
          install -D -m755 ./target/release/backend $out/bin/backend
        '';

        doCheck = true;
        checkPhase = ''
          cargo test --release -- --nocapture
        '';
      };

    };

    packages = forAllSystems (system: {
      inherit (nixpkgsFor.${system}) backend vendorCrates;
    });

    defaultPackage = forAllSystems (system: self.packages.${system}.backend);

  };
}