From 127ef6353c8766c2d7ef879eae34b49b0d9e596b Mon Sep 17 00:00:00 2001 From: Jordan Holt Date: Sun, 4 Aug 2024 11:47:33 +0100 Subject: [PATCH] Add custom packages --- flake.nix | 12 +++++- hosts/skycam/default.nix | 32 +------------- overlays/default.nix | 35 +++++++++++++++ pkgs/camera-streamer/package.nix | 74 ++++++++++++++++++++++++++++++++ pkgs/rpicam-apps/package.nix | 58 +++++++++++++++++++++++++ 5 files changed, 180 insertions(+), 31 deletions(-) create mode 100644 overlays/default.nix create mode 100644 pkgs/camera-streamer/package.nix create mode 100644 pkgs/rpicam-apps/package.nix diff --git a/flake.nix b/flake.nix index 4a3ddef..fe7a2da 100644 --- a/flake.nix +++ b/flake.nix @@ -53,14 +53,24 @@ outputs = inputs @ { self, nixpkgs, nixpkgs-unstable, agenix, deploy-rs, disko, home-manager, nixos-hardware, nixos-mailserver, ... }: let + inherit (nixpkgs) lib; + forEachSystem = lib.genAttrs [ + "x86_64-linux" + "aarch64-linux" + ]; mkPkgsForSystem = system: inputs.nixpkgs; + customPkgs = forEachSystem (system: + lib.packagesFromDirectoryRecursive { + callPackage = nixpkgs.legacyPackages.${system}.callPackage; + directory = ./pkgs; + }); overlays = [ agenix.overlays.default (import ./overlays/gnome.nix) + (import ./overlays/default.nix) ( final: prev: { unstable = import inputs.nixpkgs-unstable { system = final.system; }; - custom = self.packages { system = final.system; }; } ) ]; diff --git a/hosts/skycam/default.nix b/hosts/skycam/default.nix index d02b2a4..15aab6f 100644 --- a/hosts/skycam/default.nix +++ b/hosts/skycam/default.nix @@ -59,41 +59,13 @@ }; }; - systemd.services.ustreamer = { - enable = true; - description = "uStreamer service"; - unitConfig = { - Type = "simple"; - }; - serviceConfig = { - ExecStartPre = ''${pkgs.v4l-utils}/bin/v4l2-ctl \ - --device=/dev/video0 \ - --fix-edid-checksums \ - --info-edid - ''; - ExecStart = ''${pkgs.unstable.ustreamer}/bin/ustreamer \ - --device=/dev/video0 \ - --resolution=1280x720 \ - --format=uyvy \ - --encoder=m2m-image \ - --workers=3 \ - --persistent \ - --dv-timings \ - --drop-same-frames=30 \ - --host=0.0.0.0 \ - --port=8080 - ''; - }; - wantedBy = [ "network-online.target" ]; - confinement.mode = "chroot-only"; - }; - environment.systemPackages = with pkgs; [ + camera-streamer git neovim - libcamera libraspberrypi raspberrypi-eeprom + rpicam-apps v4l-utils unstable.ustreamer ]; diff --git a/overlays/default.nix b/overlays/default.nix new file mode 100644 index 0000000..869c5f9 --- /dev/null +++ b/overlays/default.nix @@ -0,0 +1,35 @@ +final: prev: + +/* + Generate an overlay from `pkgs` by handling the `callPackage` behaviour + ourselves, making exceptions for namespaced package sets. We cannot reuse + the definitions from `self.legacyPackages.${prev.system}`, as that would + evaluate nixpkgs twice here (prev.system does not exist then). +*/ + +let + lib = prev.lib; + + pkgs = lib.packagesFromDirectoryRecursive { + callPackage = path: overrides: path; + directory = ../pkgs; + }; +in + lib.mapAttrs + (name: value: + if lib.isAttrs value then + if lib.hasAttrByPath [ name "overrideScope" ] prev then + # Namespaced package sets created with `lib.makeScope pkgs.newScope`. + prev.${name}.overrideScope (final': prev': + lib.mapAttrs (name': value': final'.callPackage value' { }) value) + else if lib.hasAttrByPath [ name "extend" ] prev then + # Namespaced package sets created with `lib.makeExtensible`. + prev.${name}.extend (final': prev': + lib.mapAttrs (name': value': final.callPackage value' { }) value) + else + # Namespaced package sets in regular attrsets. + prev.${name} // value + else + final.callPackage value { }) + pkgs + diff --git a/pkgs/camera-streamer/package.nix b/pkgs/camera-streamer/package.nix new file mode 100644 index 0000000..c051f9f --- /dev/null +++ b/pkgs/camera-streamer/package.nix @@ -0,0 +1,74 @@ +{ stdenv +, fetchFromGitHub + +, cmake +, gnumake +, pkg-config +, xxd + +, v4l-utils +, nlohmann_json +, ffmpegSupport ? true +, ffmpeg +, libcameraSupport ? true +, libcamera +, rtspSupport ? true +, live555 +, webrtcSupport ? true +, openssl + +, lib +}: + +stdenv.mkDerivation (finalAttrs: { + pname = "camera-streamer"; + version = "0.2.8"; + + src = fetchFromGitHub { + owner = "ayufan"; + repo = "camera-streamer"; + rev = "refs/tags/v${finalAttrs.version}"; + hash = "sha256-8vV8BMFoDeh22I1/qxk6zttJROaD/lrThBxXHZSPpT4="; + fetchSubmodules = true; + }; + + # Second replacement fixes literal newline in generated version.h. + postPatch = '' + substituteInPlace Makefile \ + --replace '/usr/local/bin' '/bin' \ + --replace 'echo "#define' 'echo -e "#define' + ''; + + env.NIX_CFLAGS_COMPILE = builtins.toString [ + "-Wno-error=stringop-overflow" + "-Wno-error=format" + "-Wno-format" + "-Wno-format-security" + "-Wno-error=unused-result" + ]; + + nativeBuildInputs = [ + cmake + gnumake + pkg-config + xxd + ]; + + dontUseCmakeConfigure = true; + + buildInputs = [ nlohmann_json v4l-utils ] + ++ (lib.optional ffmpegSupport ffmpeg) + ++ (lib.optional libcameraSupport libcamera) + ++ (lib.optional rtspSupport live555) + ++ (lib.optional webrtcSupport openssl); + + installFlags = [ "DESTDIR=${builtins.placeholder "out"}" ]; + preInstall = "mkdir -p $out/bin"; + + meta = with lib; { + description = "High-performance low-latency camera streamer for Raspberry Pi's"; + website = "https://github.com/ayufan/camera-streamer"; + license = licenses.gpl3Only; + }; +}) + diff --git a/pkgs/rpicam-apps/package.nix b/pkgs/rpicam-apps/package.nix new file mode 100644 index 0000000..b38d711 --- /dev/null +++ b/pkgs/rpicam-apps/package.nix @@ -0,0 +1,58 @@ +{ stdenv +, fetchFromGitHub +, meson +, ninja +, pkg-config +, boost +, ffmpeg +, libcamera +, libdrm +, libexif +, libjpeg +, libpng +, libtiff +, lib +}: + +stdenv.mkDerivation (finalAttrs: { + pname = "rpicam-apps"; + version = "1.4.1"; + + src = fetchFromGitHub { + owner = "raspberrypi"; + repo = "rpicam-apps"; + rev = "v" + finalAttrs.version; + hash = "sha256-3NG2ZE/Ub3lTbfne0LCXuDgLGTPaAAADRdElEbZwvls="; + }; + + nativeBuildInputs = [ + meson + ninja + pkg-config + ]; + + buildInputs = [ + boost + ffmpeg + libcamera + libdrm + libexif + libjpeg + libpng + libtiff + ]; + + # Meson is no longer able to pick up Boost automatically: + # https://github.com/NixOS/nixpkgs/issues/86131 + BOOST_INCLUDEDIR = "${lib.getDev boost}/include"; + BOOST_LIBRARYDIR = "${lib.getLib boost}/lib"; + + meta = with lib; { + description = '' + libcamera-based applications to drive the cameras on a Raspberry Pi platform + ''; + homepage = "https://github.com/raspberrypi/rpicam-apps"; + license = licenses.bsd2; + }; +}) +