From 337c8928110d6694a4b9767bc23063e69a133a78 Mon Sep 17 00:00:00 2001 From: Andrii Panasiuk Date: Sun, 10 Sep 2023 00:46:37 +0300 Subject: [PATCH] fix(klipper): mostly working camera --- .circleci/config.yml | 11 ++- README.md | 7 +- cells/klipper/packages/camera-streamer.nix | 79 +++++++++++++++++++ cells/klipper/sources/generated.json | 36 +++++++-- cells/klipper/sources/generated.nix | 27 +++++-- cells/klipper/sources/nvfetcher.toml | 5 ++ cells/nixos/hosts/voron/_camera.nix | 5 +- .../hosts/voron/_hardware-configuration.nix | 53 ++++--------- .../hosts/voron/_klipper/klipper-screen.nix | 24 +++++- cells/nixos/hosts/voron/_minimize.nix | 28 +++++++ cells/nixos/hosts/voron/default.nix | 8 +- flake.lock | 17 ++++ flake.nix | 1 + 13 files changed, 236 insertions(+), 65 deletions(-) create mode 100644 cells/klipper/packages/camera-streamer.nix create mode 100644 cells/nixos/hosts/voron/_minimize.nix diff --git a/.circleci/config.yml b/.circleci/config.yml index 04a97a42..7ed32aff 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -11,7 +11,9 @@ workflows: only: - master - auto/upgrade-dependencies - + matrix: + parameters: + host: [voron, oracle] orbs: nix: eld/nix@1.1.1 @@ -19,10 +21,13 @@ jobs: build: machine: image: ubuntu-2204:2023.07.1 + parameters: + host: + type: string resource_class: arm.large steps: - nix/install: - channels: nixpkgs=https://nixos.org/channels/nixpkgs-unstable + channels: nixpkgs=https://nixos.org/channels/nixos-23.05 extra-conf: | experimental-features = flakes nix-command - nix/install-cachix @@ -38,7 +43,7 @@ jobs: - run: name: Build system command: | - nix build ".#nixosConfigurations.nixos-voron.config.system.build.toplevel" + nix build ".#nixosConfigurations.nixos-<< parameters.hots >>.config.system.build.toplevel" - run: name: Push cache command: | diff --git a/README.md b/README.md index dcb3d09a..32470a66 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ This repository is home to the nix code that builds my systems. [![nas](https://img.shields.io/github/actions/workflow/status/truelecter/hive/build-nas.yaml?event=push&logo=nixos&logoColor=white&label=nas)][nas] [![squadbook](https://img.shields.io/cirrus/github/truelecter/hive?label=squadbook&logo=nixos&logoColor=white&task=Build%20squadbook)][squadbook] [![oracle](https://img.shields.io/circleci/build/github/truelecter/hive/master?logo=nixos&logoColor=white&label=oracle)][oracle] +[![voron](https://img.shields.io/circleci/build/github/truelecter/hive/master?logo=nixos&logoColor=white&label=voron)][voron] ## Why Nix? @@ -47,5 +48,9 @@ darwin-rebuild switch --flake . -[oracle]: https://app.circleci.com/pipelines/github/truelecter/hive?branch=master [squadbook]: https://cirrus-ci.com/github/truelecter/hive/ + + + +[voron]: https://app.circleci.com/pipelines/github/truelecter/hive?branch=master +[oracle]: https://app.circleci.com/pipelines/github/truelecter/hive?branch=master diff --git a/cells/klipper/packages/camera-streamer.nix b/cells/klipper/packages/camera-streamer.nix new file mode 100644 index 00000000..9d37011d --- /dev/null +++ b/cells/klipper/packages/camera-streamer.nix @@ -0,0 +1,79 @@ +{ + lib, + stdenv, + sources, + # tools + pkg-config, + ccache, + unixtools, + cmake, + # dependencies + nlohmann_json, + ffmpeg, + libcamera, + live555, + gnutls, + # Build flags + useHWH264 ? true, + useFfmpeg ? false, + useLibcamera ? false, + useRtsp ? false, + useLibdatachannel ? true, + ... +}: let + inherit (lib) optionals optional; +in + stdenv.mkDerivation rec { + pname = "camera-streamer"; + + inherit (sources.camera-streamer) version src; + + # NIX_DEBUG = 1; + # NIX_CFLAGS_COMPILE = "-Wno-error"; + + hardeningDisable = ["all"]; + + makeFlags = + [ + "GIT_VERSION=${version}" + "GIT_REVISION=${version}" + ] + ++ optional useHWH264 "USE_HW_H264=1" + ++ optional useFfmpeg "USE_FFMPEG=1" + ++ optional useLibcamera "USE_LIBCAMERA=1" + ++ optional useRtsp "USE_RTSP=1"; + + nativeBuiltInputs = [pkg-config ccache unixtools.xxd cmake]; + + buildInputs = + [nlohmann_json] + ++ optional useFfmpeg ffmpeg + ++ optional useLibcamera libcamera + ++ optional useRtsp live555 + ++ optional useLibdatachannel gnutls; + + configurePhase = '' + echo '#define GIT_VERSION "${version}"' > version.h + echo '#define GIT_REVISION "${version}"' >> version.h + + sed -i 's/all: version/all:/g' Makefile + export PATH="${pkg-config}/bin:${unixtools.xxd}/bin:${cmake}/bin::$PATH" + ''; + + installPhase = '' + $preInstallPhase + + mkdir -p $out/bin + + cp camera-streamer $out/bin/ + + $postInstallPhase + ''; + + meta = with lib; { + description = "Touchscreen GUI that interfaces with Klipper via Moonraker"; + homepage = "https://klipperscreen.readthedocs.io/en/latest/"; + platforms = platforms.linux; + license = licenses.gpl3Only; + }; + } diff --git a/cells/klipper/sources/generated.json b/cells/klipper/sources/generated.json index 20a65113..56fc1324 100644 --- a/cells/klipper/sources/generated.json +++ b/cells/klipper/sources/generated.json @@ -1,4 +1,24 @@ { + "camera-streamer": { + "cargoLocks": null, + "date": null, + "extract": null, + "name": "camera-streamer", + "passthru": null, + "pinned": false, + "src": { + "deepClone": false, + "fetchSubmodules": true, + "leaveDotGit": false, + "name": null, + "owner": "ayufan", + "repo": "camera-streamer", + "rev": "v0.2.6", + "sha256": "sha256-m47fXOZuStaRPLJYGZKpg6RBPfFSkCafdqujagXTDXo=", + "type": "github" + }, + "version": "v0.2.6" + }, "klipper": { "cargoLocks": null, "date": "2023-08-21", @@ -41,7 +61,7 @@ }, "klipper-happy-hare": { "cargoLocks": null, - "date": "2023-09-05", + "date": "2023-09-09", "extract": null, "name": "klipper-happy-hare", "passthru": null, @@ -53,11 +73,11 @@ "name": null, "owner": "moggieuk", "repo": "Happy-Hare", - "rev": "6416e02201701c6b8e63a18a32d9f046ce2e16a5", - "sha256": "sha256-W3v3aB8vhCPRKaoLMg1dxnHhHlHMANRSL4kmyvch2Qs=", + "rev": "bb8eef4e7741db91a11e932c23d9eb40cfffe3b5", + "sha256": "sha256-YU+okOaXoVCW9Df3mZRKKIyNQ8p99OsM/S8GXC2gvls=", "type": "github" }, - "version": "6416e02201701c6b8e63a18a32d9f046ce2e16a5" + "version": "bb8eef4e7741db91a11e932c23d9eb40cfffe3b5" }, "klipper-kamp": { "cargoLocks": null, @@ -121,7 +141,7 @@ }, "klipper-screen": { "cargoLocks": null, - "date": "2023-09-06", + "date": "2023-09-09", "extract": null, "name": "klipper-screen", "passthru": null, @@ -133,11 +153,11 @@ "name": null, "owner": "jordanruthe", "repo": "KlipperScreen", - "rev": "14d3fca31c2157e505f332a94e2a1e3f528a00d4", - "sha256": "sha256-jZwCXgexupQRQ0gfxkWg/rShl2Ml3jZ88nmiXeIbD1w=", + "rev": "f8da43c37966287e1e026d30d1c28873c92a2dcb", + "sha256": "sha256-Sw/+8cMWHmn4dHY03oUmTV5F+BP1TSU7zadQx42vi8Y=", "type": "github" }, - "version": "14d3fca31c2157e505f332a94e2a1e3f528a00d4" + "version": "f8da43c37966287e1e026d30d1c28873c92a2dcb" }, "klipper-z-calibration": { "cargoLocks": null, diff --git a/cells/klipper/sources/generated.nix b/cells/klipper/sources/generated.nix index 236525ba..75088617 100644 --- a/cells/klipper/sources/generated.nix +++ b/cells/klipper/sources/generated.nix @@ -1,6 +1,17 @@ # This file was generated by nvfetcher, please do not modify it manually. { fetchgit, fetchurl, fetchFromGitHub, dockerTools }: { + camera-streamer = { + pname = "camera-streamer"; + version = "v0.2.6"; + src = fetchFromGitHub { + owner = "ayufan"; + repo = "camera-streamer"; + rev = "v0.2.6"; + fetchSubmodules = true; + sha256 = "sha256-m47fXOZuStaRPLJYGZKpg6RBPfFSkCafdqujagXTDXo="; + }; + }; klipper = { pname = "klipper"; version = "5f990f93d533247d3a675e8c423280f4333ad8ce"; @@ -27,15 +38,15 @@ }; klipper-happy-hare = { pname = "klipper-happy-hare"; - version = "6416e02201701c6b8e63a18a32d9f046ce2e16a5"; + version = "bb8eef4e7741db91a11e932c23d9eb40cfffe3b5"; src = fetchFromGitHub { owner = "moggieuk"; repo = "Happy-Hare"; - rev = "6416e02201701c6b8e63a18a32d9f046ce2e16a5"; + rev = "bb8eef4e7741db91a11e932c23d9eb40cfffe3b5"; fetchSubmodules = false; - sha256 = "sha256-W3v3aB8vhCPRKaoLMg1dxnHhHlHMANRSL4kmyvch2Qs="; + sha256 = "sha256-YU+okOaXoVCW9Df3mZRKKIyNQ8p99OsM/S8GXC2gvls="; }; - date = "2023-09-05"; + date = "2023-09-09"; }; klipper-kamp = { pname = "klipper-kamp"; @@ -74,15 +85,15 @@ }; klipper-screen = { pname = "klipper-screen"; - version = "14d3fca31c2157e505f332a94e2a1e3f528a00d4"; + version = "f8da43c37966287e1e026d30d1c28873c92a2dcb"; src = fetchFromGitHub { owner = "jordanruthe"; repo = "KlipperScreen"; - rev = "14d3fca31c2157e505f332a94e2a1e3f528a00d4"; + rev = "f8da43c37966287e1e026d30d1c28873c92a2dcb"; fetchSubmodules = false; - sha256 = "sha256-jZwCXgexupQRQ0gfxkWg/rShl2Ml3jZ88nmiXeIbD1w="; + sha256 = "sha256-Sw/+8cMWHmn4dHY03oUmTV5F+BP1TSU7zadQx42vi8Y="; }; - date = "2023-09-06"; + date = "2023-09-09"; }; klipper-z-calibration = { pname = "klipper-z-calibration"; diff --git a/cells/klipper/sources/nvfetcher.toml b/cells/klipper/sources/nvfetcher.toml index 70f8d075..c04276c9 100644 --- a/cells/klipper/sources/nvfetcher.toml +++ b/cells/klipper/sources/nvfetcher.toml @@ -32,6 +32,11 @@ fetch.pypi = "wsaccel" src.manual = "13.5" fetch.pypi = "python-telegram-bot" +[camera-streamer] +src.github = "ayufan/camera-streamer" +fetch.github = "ayufan/camera-streamer" +git.fetchSubmodules = true + # mainsail [mainsail] src.git = "https://github.com/mainsail-crew/mainsail" diff --git a/cells/nixos/hosts/voron/_camera.nix b/cells/nixos/hosts/voron/_camera.nix index 6df333fb..a0ec1b11 100644 --- a/cells/nixos/hosts/voron/_camera.nix +++ b/cells/nixos/hosts/voron/_camera.nix @@ -7,6 +7,7 @@ }: { environment.systemPackages = [ pkgs.v4l-utils + pkgs.camera-streamer ]; tl.services.tailscale-tls.enable = true; @@ -16,8 +17,8 @@ settings = { ffmpeg.bin = "${lib.getBin pkgs.ffmpeg_6-full}/bin/ffmpeg"; streams = { - nozzle = "ffmpeg:device?video=/dev/v4l/by-id/usb-XCG-221208-J_3DO_NOZZLE_CAMERA_4K_01.00.00-video-index0&video_size=800x600#video=h264"; - printer = "ffmpeg:device?video=/dev/v4l/by-id/usb-046d_HD_Pro_Webcam_C920_89E7787F-video-index0&video_size=800x600#rotate=180#video=h264"; + nozzle = "ffmpeg:device?video=/dev/v4l/by-id/usb-XCG-221208-J_3DO_NOZZLE_CAMERA_4K_01.00.00-video-index0&video_size=640x480&framerate=30&input_format=yuyv422#video=h264#hardware=v4l2m2m"; + printer = "ffmpeg:device?video=/dev/v4l/by-id/usb-046d_HD_Pro_Webcam_C920_89E7787F-video-index0&video_size=640x480&framerate=30&input_format=h264#rotate=180#video=h264#hardware=v4l2m2mls"; }; log = { format = "text"; diff --git a/cells/nixos/hosts/voron/_hardware-configuration.nix b/cells/nixos/hosts/voron/_hardware-configuration.nix index d276fce7..43a7d340 100644 --- a/cells/nixos/hosts/voron/_hardware-configuration.nix +++ b/cells/nixos/hosts/voron/_hardware-configuration.nix @@ -6,16 +6,7 @@ ... }: { boot = { - kernelPackages = pkgs.linuxPackages_rpi4.extend (lib.const (ksuper: { - kernel = ksuper.kernel.override { - autoModules = false; - structuredExtraConfig = with lib.kernel; { - CONFIG_USB_XHCI_PCI = yes; - CONFIG_USB_XHCI_PCI_RENESAS = yes; - }; - }; - })); - # kernelPackages = pkgs.linuxPackages_rpi4; + kernelPackages = pkgs.linuxPackages_rpi4; loader = { grub.enable = false; generic-extlinux-compatible = { @@ -32,23 +23,27 @@ }; consoleLogLevel = 7; initrd.availableKernelModules = [ + "xhci_hcd" + "xhci-pci-renesas" + "usbhid" "usb_storage" + + "sdhci_pci" + "mmc_block" + + "simplefb" + "pcie-brcmstb" + "vc4" "pcie_brcmstb" # required for the pcie bus to work "reset-raspberrypi" # required for vl805 firmware to load ]; - kernelParams = ["console=ttyS0,115200n8" "console=tty1" "video=DSI-1:800x480@60" "cma=448M"]; - # kernelPatches = [ - # { - # name = "xhci"; - # patch = null; - # extraStructuredConfig = { - # CONFIG_USB_XHCI_PCI = lib.kernel.yes; - # CONFIG_USB_XHCI_PCI_RENESAS = lib.kernel.yes; - # }; - # } - # ]; + kernelParams = [ + "console=ttyS0,115200n8" + "console=tty1" + "video=DSI-1:800x480@60" + ]; }; fileSystems = { @@ -63,16 +58,9 @@ }; }; - # powerManagement.cpuFreqGovernor = "performance"; + powerManagement.cpuFreqGovernor = "performance"; hardware = { - # raspberry-pi."4" = { - # i2c0.enable = false; - # i2c1.enable = false; - # fkms-3d.enable = false; - # touch-ft5406.enable = false; - # }; - enableRedistributableFirmware = true; }; @@ -84,14 +72,7 @@ environment.etc."u-boot-cm4".source = pkgs.ubootRaspberryPi4_64bit.override { extraConfig = '' - #CONFIG_CMD_NVME=y - #CONFIG_NVME=y - #CONFIG_NVME_PCI=y CONFIG_USB_STORAGE=y - CONFIG_USB_FUNCTION_MASS_STORAGE=y - CONFIG_USB_EHCI_HCD=y - CONFIG_USB_EHCI_GENERIC=y - CONFIG_USB_OHCI_HCD=y CONFIG_USB_XHCI_BRCM=y ''; extraPatches = [ diff --git a/cells/nixos/hosts/voron/_klipper/klipper-screen.nix b/cells/nixos/hosts/voron/_klipper/klipper-screen.nix index 728ab3bb..a9f06497 100644 --- a/cells/nixos/hosts/voron/_klipper/klipper-screen.nix +++ b/cells/nixos/hosts/voron/_klipper/klipper-screen.nix @@ -78,12 +78,28 @@ hardware.deviceTree = { filter = "bcm2711-rpi-cm4.dtb"; overlays = [ - # rpi-ft5406 + # { + # name = "rpi-ft5406"; + # dtboFile = "${pkgs.device-tree_rpi.overlays}/rpi-ft5406.dtbo"; + # } { - name = "rpi-ft5406"; - dtboFile = "${pkgs.device-tree_rpi.overlays}/rpi-ft5406.dtbo"; - } + name = "xhci-fix"; + dtsText = '' + /dts-v1/; + /plugin/; + / { + compatible = "brcm,bcm2711"; + fragment@0 { + //target-path = "/scb/xhci@7e9c0000"; + target = <&xhci>; + __overlay__ { + status = "okay"; + }; + }; + }; + ''; + } { name = "disable-bt"; dtboFile = "${pkgs.device-tree_rpi.overlays}/disable-bt.dtbo"; diff --git a/cells/nixos/hosts/voron/_minimize.nix b/cells/nixos/hosts/voron/_minimize.nix new file mode 100644 index 00000000..371268bf --- /dev/null +++ b/cells/nixos/hosts/voron/_minimize.nix @@ -0,0 +1,28 @@ +# https://discourse.nixos.org/t/how-to-have-a-minimal-nixos/22652/4 +{ + modulesPath, + pkgs, + ... +}: { + disabledModules = [ + "${modulesPath}/profiles/all-hardware.nix" + "${modulesPath}/profiles/base.nix" + ]; + + environment.systemPackages = [ + pkgs.pciutils + pkgs.usbutils + ]; + + # environment.noXlibs = true; + documentation.enable = false; + documentation.doc.enable = false; + documentation.info.enable = false; + documentation.man.enable = false; + documentation.nixos.enable = false; + + programs.command-not-found.enable = false; + + boot.initrd.includeDefaultModules = false; + environment.defaultPackages = [pkgs.perl]; +} diff --git a/cells/nixos/hosts/voron/default.nix b/cells/nixos/hosts/voron/default.nix index 26e015fe..a1469a4e 100644 --- a/cells/nixos/hosts/voron/default.nix +++ b/cells/nixos/hosts/voron/default.nix @@ -10,7 +10,7 @@ in { suites.base profiles.common.networking.tailscale - profiles.faster-linux + # profiles.faster-linux inputs.cells.klipper.nixosModules.klipper @@ -20,18 +20,20 @@ in { ./_wifi.nix ./_camera.nix ./_klipper + ./_minimize.nix ]; bee.system = system; bee.home = inputs.home; - bee.pkgs = import inputs.latest { + bee.pkgs = import inputs.printers { inherit system; config.allowUnfree = true; overlays = [ inputs.cells.klipper.overlays.klipper ( - _:prev: { + _: prev: { deviceTree.applyOverlays = prev.callPackage ./_dtmerge.nix {}; + makeModulesClosure = x: prev.makeModulesClosure (x // {allowMissing = true;}); } ) ]; diff --git a/flake.lock b/flake.lock index 86102e73..6526d702 100644 --- a/flake.lock +++ b/flake.lock @@ -888,6 +888,22 @@ "type": "github" } }, + "printers": { + "locked": { + "lastModified": 1693985761, + "narHash": "sha256-K5b+7j7Tt3+AqbWkcw+wMeqOAWyCD1MH26FPZyWXpdo=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "0bffda19b8af722f8069d09d8b6a24594c80b352", + "type": "github" + }, + "original": { + "owner": "nixos", + "repo": "nixpkgs", + "rev": "0bffda19b8af722f8069d09d8b6a24594c80b352", + "type": "github" + } + }, "root": { "inputs": { "colmena": "colmena", @@ -911,6 +927,7 @@ "nixos" ], "nvfetcher": "nvfetcher", + "printers": "printers", "sops-nix": "sops-nix", "std": "std" } diff --git a/flake.nix b/flake.nix index 80a90ed5..8af23c60 100644 --- a/flake.nix +++ b/flake.nix @@ -85,6 +85,7 @@ inputs = { latest.url = "github:nixos/nixpkgs/nixos-unstable"; k8s.url = "github:nixos/nixpkgs/3005f20ce0aaa58169cdee57c8aa12e5f1b6e1b3"; + printers.url = "github:nixos/nixpkgs/0bffda19b8af722f8069d09d8b6a24594c80b352"; nixos.url = "github:nixos/nixpkgs/release-23.05"; nixpkgs.follows = "nixos";