flake.nix

140 lines · 4193 bytes

  1{
  2  description = "iamb";
  3  nixConfig.bash-prompt = "[nix-develop]$ ";
  4
  5  inputs = {
  6    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  7    flake-utils.url = "github:numtide/flake-utils";
  8    crane.url = "github:ipetkov/crane";
  9    fenix = {
 10      url = "github:nix-community/fenix";
 11      inputs.nixpkgs.follows = "nixpkgs";
 12    };
 13  };
 14
 15  outputs = {
 16    self,
 17    nixpkgs,
 18    crane,
 19    flake-utils,
 20    fenix,
 21    ...
 22  }:
 23    flake-utils.lib.eachDefaultSystem (
 24      system: let
 25        pkgs = nixpkgs.legacyPackages.${system};
 26        inherit (pkgs) lib;
 27
 28        rustToolchain = fenix.packages.${system}.fromToolchainFile {
 29          file = ./rust-toolchain.toml;
 30          # When the file changes, this hash must be updated.
 31          sha256 = "sha256-h+t2xTBz5yt2YIO+1VMIIGlCU7gyp2LYOFvaV1nwOXU=";
 32        };
 33
 34        # Nightly toolchain for rustfmt (pinned to current flake lock)
 35        # Note that the github CI uses "current nightly" for formatting, it 's not pinned.
 36        rustNightly = fenix.packages.${system}.latest;
 37
 38        craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
 39        craneLibNightly = (crane.mkLib pkgs).overrideToolchain rustNightly.toolchain;
 40
 41        src = lib.fileset.toSource {
 42          root = ./.;
 43          fileset = lib.fileset.unions [
 44            (craneLib.fileset.commonCargoSources ./.)
 45            ./src/windows/welcome.md
 46
 47            ./docs/iamb.1
 48            ./docs/iamb.5
 49            ./docs/iamb.svg
 50            ./docs/iamb.metainfo.xml
 51            ./iamb.desktop
 52          ];
 53        };
 54
 55        commonArgs = rec {
 56          inherit src;
 57          strictDeps = true;
 58          pname = "iamb";
 59          version = self.shortRev or self.dirtyShortRev;
 60
 61          VERGEN_GIT_SHA = version;
 62        };
 63
 64        # Build *just* the cargo dependencies, so we can reuse
 65        # all of that work (e.g. via cachix) when running in CI
 66        cargoArtifacts = craneLib.buildDepsOnly commonArgs;
 67
 68        # Build the actual crate
 69        iamb = craneLib.buildPackage (commonArgs // {
 70          inherit cargoArtifacts;
 71
 72          nativeBuildInputs = [ pkgs.installShellFiles ];
 73          postInstall = ''
 74            mkdir -p completions
 75            ./target/release/iamb --completions zsh > completions/_iamb
 76            ./target/release/iamb --completions bash > completions/iamb.bash
 77            ./target/release/iamb --completions fish > completions/iamb.fish
 78            installShellCompletion --cmd iamb completions/*
 79
 80            installManPage $src/docs/iamb.{1,5}
 81            install -D $src/docs/iamb.svg -t $out/share/icons/hicolor/scalable/apps
 82            install -D $src/docs/iamb.metainfo.xml $out/share/appdata/chat.iamb.iamb.appdata.xml
 83            install -D $src/iamb.desktop -t $out/share/applications
 84          '';
 85
 86          meta = {
 87            description = "Matrix client for Vim addicts";
 88            mainProgram = "iamb";
 89            homepage = "https://github.com/ulyssa/iamb";
 90            license = lib.licenses.asl20;
 91          };
 92        });
 93      in
 94      {
 95        checks = {
 96          # Build the crate as part of `nix flake check`
 97          inherit iamb;
 98
 99          iamb-clippy = craneLib.cargoClippy (commonArgs
100            // {
101              inherit cargoArtifacts;
102              cargoClippyExtraArgs = "--all-targets -- --deny warnings";
103            });
104
105          iamb-fmt = craneLibNightly.cargoFmt {
106            inherit src;
107          };
108
109          iamb-nextest = craneLib.cargoNextest (commonArgs
110            // {
111              inherit cargoArtifacts;
112              partitions = 1;
113              partitionType = "count";
114            });
115        };
116
117        packages.default = iamb;
118
119        apps.default = flake-utils.lib.mkApp {
120          drv = iamb;
121        };
122
123        devShells.default = craneLib.devShell {
124          # Inherit inputs from checks
125          checks = self.checks.${system};
126
127          packages = with pkgs; [
128            cargo-tarpaulin
129            cargo-watch
130            sqlite
131          ];
132
133          shellHook = ''
134            # Prepend nightly rustfmt to PATH.
135            export PATH="${rustNightly.rustfmt}/bin:$PATH"
136          '';
137        };
138      }
139    );
140}