+/tetris

my tetris implementation

git clone https://git.julia.blue/+/tetris

Julia RyanDownload assets from urls19928ec

change-origin
4.9 KiB125 linesraw
1{
2  description = "tetris and related tools";
3  inputs = {
4    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
5    flake-utils.url = "github:numtide/flake-utils";
6    crane.url = "github:ipetkov/crane";
7    rust-overlay.url = "github:oxalica/rust-overlay";
8    rust-overlay.inputs.nixpkgs.follows = "nixpkgs";
9  };
10
11  outputs = { self, nixpkgs, flake-utils, crane, rust-overlay, ... }:
12    flake-utils.lib.eachDefaultSystem (system: let
13      pkgs = import nixpkgs {
14          inherit system;
15          overlays = [ (import rust-overlay) ];
16        };
17      craneLib = (crane.mkLib pkgs).overrideToolchain (p:
18        p.rust-bin.stable.latest.default);
19      inherit (pkgs) lib;
20
21      darwinArgs = {
22        # `coreaudio-sys` calls `bindgen` at build time _always_ 😞
23        LIBCLANG_PATH="${pkgs.llvmPackages.libclang.lib}/lib";
24
25        # `coreaudio-sys` expects the headers for this packages to be available
26        # in a directory structure matching the MacOS SDKs.
27        #
28        # If we don't set this env var, `coreaudio-sys` will ask `xcrun` for the
29        # MacOS SDK path and the nix `xcrun` wrapper just points to a stub SDK
30        # without any frameworks or headers: https://github.com/NixOS/nixpkgs/blob/bcf1085724f62e860f2cddd2c6eaee7dceb22888/pkgs/development/tools/xcbuild/wrapper.nix#L54
31        #
32        # See:
33        #  - https://discourse.nixos.org/t/develop-shell-environment-setup-for-macos/11399/6
34        #  - https://github.com/RustAudio/coreaudio-sys/blob/8185e0704754a0d3e3c41e9557d24f5f406ce5ef/build.rs#L6
35        COREAUDIO_SDK_PATH = pkgs.symlinkJoin {
36          name = "sdk";
37          # See: https://github.com/RustAudio/coreaudio-sys/blob/8185e0704754a0d3e3c41e9557d24f5f406ce5ef/build.rs#L50-L102
38          paths = with pkgs.darwin.apple_sdk.frameworks;
39            [ AudioToolbox AudioUnit CoreAudio CoreFoundation CoreMIDI OpenAL ];
40          postBuild = ''
41            mkdir $out/System
42            mv $out/Library $out/System
43          '';
44        };
45      };
46
47      addBindgenEnvVar = base: base.overrideAttrs (old: {
48          # (the above fixes for the macOS framework paths do not seem to be
49          # enough anymore; poking at `cbindgen` reveals that it now seems to
50          # "sanitize" our NIX_CFLAGS_COMPILE injected `-iframework` includes
51          # and turn them into regular `-isystem` includes, breaking
52          # framework headers. to get around this we use the inelegant
53          # "big hammer" solution below: forcibly reintroducing our
54          # `-iframework` flags to `cbindgen`'s list of flags passed to
55          # `libclang`):
56          preBuild = (old.preBuild or "") +
57            pkgs.lib.optionalString (pkgs.stdenv.isDarwin) ''
58              export BINDGEN_EXTRA_CLANG_ARGS="$NIX_CFLAGS_COMPILE"
59            '';
60      });
61
62      src = craneLib.cleanCargoSource ./.;
63
64      commonArgs = {
65        inherit src;
66        strictDeps = true;
67
68        nativeBuildInputs = lib.optionals pkgs.stdenv.isLinux (with pkgs; [
69          pkg-config makeWrapper alsa-lib openssl
70        ]);
71
72        buildInputs = with pkgs; [ libiconv ] ++
73          (lib.optional (stdenv.isLinux) alsa-lib) ++
74          (lib.optionals (stdenv.isDarwin)
75            (with darwin.apple_sdk.frameworks; [ AudioUnit CoreAudio pkgs.xcbuild ]));
76      } // (pkgs.lib.optionalAttrs (pkgs.stdenv.isDarwin) darwinArgs);
77
78      cargoArtifacts = addBindgenEnvVar (craneLib.buildDepsOnly commonArgs);
79
80      tetris = craneLib.buildPackage (commonArgs // {
81        inherit cargoArtifacts;
82        doCheck = false; # there's a separate check for that
83      } // (pkgs.lib.optionalAttrs (pkgs.stdenv.isLinux) {
84        postInstall = ''
85          wrapProgram $out/bin/tetris \
86            --set-default "ALSA_PLUGIN_DIR" "${pkgs.alsa-plugins}/lib/alsa-lib"
87        '';
88      }));
89
90      # jstrisSprint = pkgs.writeShellApplication {
91      #   name = "jstris-sprint";
92      #   runtimeInputs = jjstrisScriptDeps;
93      #   text = ./jstris-sprint.sh;
94      # };
95
96      jstrisScriptDeps = with pkgs; [
97        xh pup jq bc sd choose moreutils gnuplot
98        (python3.withPackages (pp: with pp;[matplotlib]))
99      ];
100
101    in {
102      packages.default = tetris;
103
104      checks = {
105        build = tetris;
106        test = craneLib.cargoNextest (commonArgs // { inherit cargoArtifacts; });
107        clippy = addBindgenEnvVar (craneLib.cargoClippy (commonArgs // {
108          inherit cargoArtifacts;
109          cargoClippyExtraArgs = "--all-targets -- --deny warnings";
110        }));
111        doc = craneLib.cargoDoc (commonArgs // { inherit cargoArtifacts; });
112        fmt = craneLib.cargoFmt { inherit src; };
113        toml-fmt = craneLib.taploFmt {
114          src = pkgs.lib.sources.sourceFilesBySuffices src [ ".toml" ];
115        };
116      };
117
118      devShells.default = craneLib.devShell {
119        inputsFrom = [ tetris ];
120        checks = self.checks.${system};
121        packages = with pkgs; [ rust-analyzer ] ++ jstrisScriptDeps;
122      };
123    }
124  );
125}