summaryrefslogtreecommitdiff
path: root/flake.nix
blob: e035475465cebd7c34b92b38f6b8b1bbd3f94db3 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
{
  inputs = {
    nixpkgs = {
      url = "github:NixOS/nixpkgs/nixos-unstable";
    };
    nixgl = {
      url = "github:nix-community/nixGL";
    };
    nixos-hardware = {
      url = "github:NixOS/nixos-hardware/master";
    };
    nixos-wsl = {
      url = "github:nix-community/NixOS-WSL/main";
    };
    home-manager = {
      url = "github:nix-community/home-manager/master";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, nixgl, nixos-hardware, nixos-wsl, home-manager }: let
    overlays = [
      (self: super: {
        libplacebo = super.libplacebo.overrideAttrs (oldAttrs: {
          patches = [
            ./subprojects/packagefiles/libplacebo/cache_ref_rect.diff
            ./subprojects/packagefiles/libplacebo/fix_rotate.diff
            ./subprojects/packagefiles/libplacebo/glClientWaitSync_0_return.diff
            ./subprojects/packagefiles/libplacebo/info_priv_hash.diff
          ];
        });
        #libass = super.libass.overrideAttrs (oldAttrs: { patches = []; });
        libcue = super.libcue.overrideAttrs (oldAttrs: {
          patches = [
            ./subprojects/packagefiles/libcue/55.diff
            # https://github.com/lipnitsk/libcue/pull/4
            ./subprojects/packagefiles/libcue/composer_rem.diff
          ];
        });
        ffmpeg_6 = super.ffmpeg_6.overrideAttrs (oldAttrs: {
          patches = oldAttrs.patches ++ [
            ./env/nixpkgs-patches/libavcodec_mdec.c.patch
          ];
        });
        notcurses = super.notcurses.overrideAttrs (oldAttrs: {
          patches = [
            ./subprojects/packagefiles/notcurses/duration_and_ffmpeg_fix.diff
          ];
        });
      })
      nixgl.overlays.default
    ];
    forAllSupportedSystems = function:
      nixpkgs.lib.genAttrs [
        "x86_64-linux"
        "i686-linux"
        "aarch64-linux"
      ] (system:
        function (import nixpkgs {
          inherit system;
          inherit overlays;
        }));
    forAllDevSystems = function:
      nixpkgs.lib.genAttrs [
        "x86_64-linux"
        "i686-linux"
        "aarch64-linux"
      ] (system:
        function (import (nixpkgs.legacyPackages.${system}.applyPatches {
          src = nixpkgs;
          name = "nixpkgs-camu";
          patches = [ ];
        }) {
          inherit system;
          inherit overlays;
          config = {
            allowUnfree = true;
            android_sdk.accept_license = true;
          };
        }));
    buildWithFlags = (pkg: flags:
      pkg.overrideAttrs (oldAttrs: {
        env.NIX_CFLAGS_COMPILE = (oldAttrs.env.NIX_CFLAGS_COMPILE or "") + " ${flags}";
      })
    );
  in rec {
    packages = forAllSupportedSystems (pkgs: {
      default = with pkgs; stdenv.mkDerivation rec {
        name = "camu";
        version = "0.01";
        src = fetchgit {
          url = "https://git.akon.city/camu.git";
          rev = "f51b7c35c95a5c6ad50c9fc430780e6953f37dad";
          postFetch = ''
            export NIX_SSL_CERT_FILE=${cacert}/etc/ssl/certs/ca-bundle.crt

            cd "$out"
            ${lib.getExe meson} subprojects download \
              libalabaster libnaunet stela miniaudio linmath.h

            cd "$out/subprojects/libalabaster-c633226"
            ${lib.getExe meson} subprojects download c89atomic

            cd "$out/subprojects/libnaunet-a389b8f"
            ${lib.getExe meson} subprojects download libev

            cd "$out"

            find subprojects -type d -name .git -prune -execdir rm -r {} +
          '';
          sha256 = "uDsUpWF3EWToRcJSiQ2F4oPElEOXjq4QsbRwi3uVilo=";
        };
        sources = [ "file" "http" "cdio" ];
        codecs = [ "ffmpeg" ];
        window = "wayland";
        renderer-api = "vulkan";
        mesonFlags = [
          "-Dsources=${lib.join "," sources}"
          "-Dcodecs=${lib.join "," codecs}"
          "-Drenderer-api=${renderer-api}"
          "-Dwindow=${window}"
          "-Dtests=disabled"
          "-Drelease-asserts=false"
        ];
        nvidia-support = false;
        fixupPhase = if nvidia-support then ''
          wrapProgram $out/bin/cmv --set XDG_DATA_DIRS "${pkgs.mesa}/share:${pkgs.linuxPackages.nvidia_x11}/share" --set __EGL_VENDOR_LIBRARY_DIRS "${pkgs.mesa}/share/glvnd/egl_vendor.d:${pkgs.linuxPackages.nvidia_x11}/share/glvnd/egl_vendor.d"
        '' else ''
          wrapProgram $out/bin/cmv --set XDG_DATA_DIRS "${pkgs.mesa}/share" --set __EGL_VENDOR_LIBRARY_DIRS "${pkgs.mesa}/share/glvnd/egl_vendor.d"
        '';
        nativeBuildInputs = [
          git
          gcc
          nasm
          pkg-config
          autoconf
          automake
          autogen
          libtool
          gnumake
          ninja
          meson
          cmake
          ((python3.override { enableOptimizations = true; }).withPackages (python-pkgs: [
            python-pkgs.setuptools
            python-pkgs.pip
            python-pkgs.cython
          ]))
          makeWrapper
        ] ++ lib.optionals (window == "wayland") [
          wayland-scanner.bin
        ] ++ lib.optionals (lib.elem "ffmpeg" codecs) [
          ffmpeg-headless # Native input for ffprobe.
        ] ++ lib.optionals (lib.elem "libdvd" codecs) [
          jdk8 # Blu-ray
        ];
        buildInputs = [
          (curl.override { websocketSupport = true; })
          jansson
          libblake3
          libass
          alsa-lib
          libpulseaudio
          libplacebo
          xxhash
          lcms
          libdovi
          notcurses
        ] ++ lib.optionals (window == "wayland") [
          wayland
          wayland-protocols
          wlr-protocols
          egl-wayland
        ] ++ lib.optionals (window == "x11") [
          xorg.libX11
          xorg.libXext
          xorg.libXrandr
          xorg.libXfixes
          xorg.libXcursor
          libxkbcommon
        ] ++ lib.optionals (window == "x11") [
          glfw3
        ] ++ lib.optionals (renderer-api == "vulkan") [
          vulkan-loader
          vulkan-headers
          vulkan-validation-layers
          shaderc
        ] ++ lib.optionals (renderer-api == "gl") [
          libGL
        ] ++ lib.optionals (lib.elem "ffmpeg" codecs) [
          soxr
          dav1d
        ] ++ lib.optionals (lib.elem "stb_image" codecs) [
          stb
        ] ++ lib.optionals (lib.elem "wuffs" codecs) [
          wuffs
        ] ++ lib.optionals (lib.elem "spng" codecs) [
          libspng
        ] ++ lib.optionals (lib.elem "cdio" sources) [
          libcdio
          libcdio-paranoia
          libcue
        ] ++ lib.optionals (lib.elem "libdvd" sources) [
          libdvdcss
          libdvdread
          libdvdnav
        ];
        meta = with lib; {
          homepage = "https://akon.city/";
          description = "";
          changelog = "";
          license = [ licenses.gpl3Only ];
          maintainers = [];
          mainProgram = "cmv";
        };
      };
      # https://cryptoisland.blog/posts/2024/12/09/flake-based-recipe-for-offline-dev-shells-in-nixos/
      shellWrapper = with pkgs; let
        shell = devShells.${stdenv.hostPlatform.system}.default;
      in pkgs.writeScriptBin "camu-devshell" ''
        export PATH=${pkgs.lib.makeBinPath shell.nativeBuildInputs}:$PATH
        export PKG_CONFIG_PATH=${pkgs.lib.makeSearchPathOutput "dev" "lib/pkgconfig" shell.buildInputs}:$PKG_CONFIG_PATH
        export PKG_CONFIG_PATH=${pkgs.lib.makeSearchPathOutput "dev" "share/pkgconfig" shell.buildInputs}:$PKG_CONFIG_PATH
        export PKG_CONFIG_PATH=${pkgs.lib.makeSearchPathOutput "dev" "lib/pkgconfig" shell.nativeBuildInputs}:$PKG_CONFIG_PATH
        export PKG_CONFIG_PATH=${pkgs.lib.makeSearchPathOutput "dev" "share/pkgconfig" shell.nativeBuildInputs}:$PKG_CONFIG_PATH
        ${shell.shellHook}
      '';
    });

    devShells = forAllDevSystems (pkgs: rec {
      default = with pkgs; mkShell rec {
        NIX_HARDENING_ENABLE = "";
        nativeBuildInputs = packages.${stdenv.hostPlatform.system}.default.nativeBuildInputs ++ [
          gdb
          clang-analyzer
          perf-tools
          valgrind
          clang-tools
          mypy
          #ruff
          #ty
          #kdePackages.kcachegrind
          #perl
          asciidoctor
          #pandoc
          #emscripten
          #makemkv
          mesa-demos
          # mesa build
          #buildPackages.mesa.cross_tools
          #mesa.depsBuildBuild
          #mesa.nativeBuildInputs
          #mesa.buildInputs
          # testing terminals
          #foot
          #alacritty
          #chafa
          #rxvt-unicode
          #st
          #xfce.xfce4-terminal
          # market research
          #imv
          #vlc
          #mpv
          #clapper
          #mpd
          #(ncmpcpp.override { visualizerSupport = true; clockSupport = false; })
          #cmus
          #clementine
          #strawberry-qt6
          #quodlibet
          #fooyin
          #deadbeef
          #sayonara
          #tauon
          #kdePackages.elisa
          #rhythmbox
          #lollypop
          #xfce.parole
        ];
        buildInputs = packages.${stdenv.hostPlatform.system}.default.buildInputs ++ [
          # glfw3 build
          xinput
          libXinerama
          # notcurses build
          libunistring
          ncurses
          # discord
          libsodium
        ];
        nvidia-support = false;
        shellHook = (if nvidia-support then ''
          export XDG_DATA_DIRS="${pkgs.mesa}/share:${pkgs.linuxPackages.nvidia_x11}/share:$XDG_DATA_DIRS"
          export __EGL_VENDOR_LIBRARY_DIRS="${pkgs.mesa}/share/glvnd/egl_vendor.d:${pkgs.linuxPackages.nvidia_x11}/share/glvnd/egl_vendor.d:$__EGL_VENDOR_LIBRARY_DIRS"
        '' else '''') + ''
          export SHELL="${pkgs.bashInteractive}/bin/bash"
          # Vulkan ICDs.
          export XDG_DATA_DIRS="${pkgs.mesa}/share:$XDG_DATA_DIRS"
          # EGL from Nix.
          export __EGL_VENDOR_LIBRARY_DIRS="${pkgs.mesa}/share/glvnd/egl_vendor.d:$__EGL_VENDOR_LIBRARY_DIRS"
          # libva from Nix.
          export LIBVA_DRIVERS_PATH="${pkgs.mesa}/lib/dri"
          export NIX_BUILD_SHELL=$SHELL
          export NIX_SHELL="camu-${stdenv.hostPlatform.system}+dev"
          export PS1="(\$NIX_SHELL)"$PS1
          echo -en '\033]2;'nix-shell: $NIX_SHELL'\007'
        '';
      };

      buildtest = with pkgs; mkShell {
        NIX_HARDENING_ENABLE = "";
        nativeBuildInputs = packages.${stdenv.hostPlatform.system}.default.nativeBuildInputs ++ [
          gdb
          clang-analyzer
          perf-tools
          valgrind
        ];
        buildInputs = packages.${stdenv.hostPlatform.system}.default.buildInputs;
        shellHook = ''
          export SHELL="${pkgs.bashInteractive}/bin/bash"
          export NIX_BUILD_SHELL=$SHELL
          export NIX_SHELL="camu-${stdenv.hostPlatform.system}"
          export PS1="(\$NIX_SHELL)"$PS1
        '';
      };

      mingw64 = with pkgs; let
        gcc = pkgsCross.ucrt64.buildPackages.wrapCCWith {
          cc = (pkgsCross.ucrt64.buildPackages.gcc.cc.override ({
            threadsCross = { model = "win32"; package = null; };
          }));
        };
        gccWin32Stdenv = overrideCC pkgsCross.ucrt64.stdenv gcc;
      in mkShell.override { stdenv = gccWin32Stdenv; } {
        # Note that `cc` has to invoke a native compiler not a cross compiler.
        # If meson complains that a compiler for the "build machine" is not found,
        # it may be due to NIX_COMPILE_FLAGS, or NIX_LDFLAGS.
        NIX_HARDENING_ENABLE = "";
        nativeBuildInputs = [
          git
          buildPackages.gcc
          nasm
          pkg-config
          autoconf
          automake
          autogen
          ninja
          meson
          cmake
          gdb
          #wineWowPackages.staging
          # Use nixGLIntel to run wine
          #pkgs.nixgl.nixGLIntel
          #pkgs.nixgl.nixVulkanIntel
          # clang-cl, .cc is the 'unwrapped' compiler.
          clang.cc
          msitools
          lld
          libllvm
          # wine build
          flex
          bison
          libGLU
          libGL
          #mesa.osmesa
          libdrm
          wayland
          wayland-scanner
          libxkbcommon
          wayland-protocols
          wayland.dev
          libxkbcommon.dev
          libgbm
          # vkd3d-proton build
          glslang
        ];
        buildInputs = with pkgsCross.ucrt64; [
          (zlib.override { shared = false; })
          (bzip2.override { enableStatic = true; enableShared = false; })
          (soxr.overrideAttrs (oldAttrs: {
            cmakeFlags = oldAttrs.cmakeFlags ++ [ "-DBUILD_STATIC_LIBS=ON" "-DBUILD_SHARED_LIBS=OFF" ];
          }))
          (dav1d.overrideAttrs (oldAttrs: {
            mesonFlags = oldAttrs.mesonFlags ++ [ "--default-library=static" ];
          }))
          (openssl.override { static = true; })
          xxhash
          (libiconv.override { enableStatic = true; enableShared = false; })
          vulkan-headers
          #(shaderc.overrideAttrs (oldAttrs: {
          #   cmakeFlags = oldAttrs.cmakeFlags ++ [ "-DBUILD_STATIC_LIBS=ON" "-DBUILD_SHARED_LIBS=OFF" ];
          #}))
        ];
        # https://old.reddit.com/r/NixOS/comments/1c4lc1n/how_to_run_this_minimal_python_script_with/kzpao62/
        shellHook = ''
          export SHELL="${pkgs.bashInteractive}/bin/bash"
          # For native compiler.
          export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${pkgs.stdenv.cc.cc.lib}/lib"
          export XDG_DATA_DIRS="${pkgs.mesa}/share:$XDG_DATA_DIRS"
          # Using x86_64-w64-mingw32-ar (as opposed to x86_64-w64-mingw32-gcc-ar) breaks lto.
          unset AR NM
          #alias wine="${pkgs.nixgl.nixGLIntel}/bin/nixGLIntel wine"
          # Silence Wine logging and enable Esync by default.
          export WINEDEBUG=-all
          export WINEESYNC=1
          export NIX_BUILD_SHELL=$SHELL
          export NIX_SHELL="camu-mingw64"
          export PS1="(\$NIX_SHELL)"$PS1
        '';
      };

      mingw32 = with pkgs; let
        gcc = pkgsCross.mingw32.buildPackages.wrapCCWith {
          cc = (pkgsCross.mingw32.buildPackages.gcc-unwrapped.override (old: {
            threadsCross = { model = "win32"; package = null; };
          }));
        };
        gccWin32Stdenv = overrideCC pkgsCross.mingw32.stdenv gcc;
      in mkShell.override { stdenv = gccWin32Stdenv; } {
        NIX_HARDENING_ENABLE = "";
        nativeBuildInputs = mingw64.nativeBuildInputs;
        buildInputs = with pkgsCross.mingw32; [
          (buildWithFlags (zlib.override { shared = false; }) "-Os")
          ((buildWithFlags bzip2 "-Os").override { enableStatic = true; enableShared = false; })
          ((buildWithFlags soxr "-Os").overrideAttrs (oldAttrs: {
            cmakeFlags = oldAttrs.cmakeFlags ++ [ "-DBUILD_STATIC_LIBS=ON" "-DBUILD_SHARED_LIBS=OFF" ];
          }))
          ((buildWithFlags openssl "-Os").override { static = true; })
          (buildWithFlags xxhash "-Os")
          ((buildWithFlags libiconv "-Os").override { enableStatic = true; enableShared = false; })
          vulkan-headers
        ];
        shellHook = ''
          export SHELL="${pkgs.bashInteractive}/bin/bash"
          export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${pkgs.stdenv.cc.cc.lib}/lib"
          alias wine="${pkgs.nixgl.nixGLIntel}/bin/nixGLIntel wine"
          export WINEDEBUG=-all
          export WINEESYNC=1
          export NIX_BUILD_SHELL=$SHELL
          export NIX_SHELL="camu-mingw32"
          export PS1="(\$NIX_SHELL)"$PS1
        '';
      };

      android = with pkgs; let
        cmakeVersion = "3.22.1";
        androidComposition = androidenv.composeAndroidPackages {
          cmdLineToolsVersion = "19.0";
          toolsVersion = "26.1.1";
          platformToolsVersion = "36.0.2";
          buildToolsVersions = [ "36.1.0" ];
          includeEmulator = false;
          emulatorVersion = "35.2.5";
          platformVersions = [ "34" ];
          includeSources = false;
          includeSystemImages = false;
          systemImageTypes = [ "google_apis_playstore" ];
          abiVersions = [ "armeabi-v7a" "arm64-v8a" ];
          cmakeVersions = [ cmakeVersion ];
          includeNDK = true;
          # latest = 28.0.13004108, _counted_by conflict.
          ndkVersion = "23.2.8568313";
          useGoogleAPIs = false;
          useGoogleTVAddOns = false;
          includeExtras = [ "extras;google;gcm" ];
        };
      in pkgs.mkShell rec {
        NIX_HARDENING_ENABLE = "";
        ANDROID_HOME = "${androidComposition.androidsdk}/libexec/android-sdk";
        ANDROID_NDK_ROOT = "${ANDROID_HOME}/ndk-bundle";
        nativeBuildInputs = [
          ninja
          meson
          androidComposition.androidsdk
        ];
        shellHook = ''
          export SHELL="${pkgs.bashInteractive}/bin/bash"
          export NIX_BUILD_SHELL=$SHELL
          export PATH="$(echo "$ANDROID_HOME/cmake/${cmakeVersion}"*/bin):$PATH"
          export PATH="$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH"
          export NIX_SHELL="camu-android"
          export PS1="(\$NIX_SHELL)"$PS1
        '';
      };
    });

    # https://github.com/nix-community/NixOS-WSL?tab=readme-ov-file#quick-start
    # sudo nix-channel --update
    # sudo cp bootstrap-configuration.nix /etc/nixos/configuration.nix
    # sudo nixos-rebuild switch
    # sudo nixos-rebuild --flake .#wsl-dev boot
    # kitty_new_window.bat
    nixosConfigurations.wsl-dev = let
      system = "x86_64-linux";
      source-pkgs = nixpkgs.legacyPackages.${system};
      nixpkgs-patched = source-pkgs.applyPatches {
        name = "nixpkgs-patched";
        src = nixpkgs;
        patches = [ ];
      };
      nixosSystem = import (nixpkgs-patched + "/nixos/lib/eval-config.nix");
    in nixosSystem {
      system = system;
      modules = [
        ({ pkgs, ... }: {
          nixpkgs.overlays = [];
          environment.systemPackages = [
            packages.${system}.default
            packages.${system}.shellWrapper
          ];
        })
        nixos-wsl.nixosModules.wsl
        home-manager.nixosModules.home-manager {
          home-manager.useGlobalPkgs = true;
          home-manager.useUserPackages = true;
        }
        ./env/wsl/wsl-dev-host.nix
      ];
    };

    # Raspberry Pi 3 Model B V1.2 + JustBoom Digi HAT
    nixosConfigurations.cmsb-001 = let
      system = "aarch64-linux";
      source-pkgs = nixpkgs.legacyPackages.${system};
      nixpkgs-patched = source-pkgs.applyPatches {
        name = "nixpkgs-patched";
        src = nixpkgs;
        patches = [ ];
      };
      nixosSystem = import (nixpkgs-patched + "/nixos/lib/eval-config.nix");
    in nixosSystem {
      system = system;
      modules = [
        ({ pkgs, ... }: {
          nixpkgs.overlays = [];
          environment.systemPackages = [
            (packages.${system}.default.overrideAttrs (oldAttrs: { mesonFlags = [
              "-Drenderer=momo"
              "-Drenderer-api=gl-rpi"
            ]; }))
            packages.${system}.shellWrapper
          ];
        })
        nixos-hardware.nixosModules.raspberry-pi-3
        ./hardware/cmsb-001/configuration.nix
      ];
    };
  };
}