From dce1df08e32f2b94c557154b5410d8cc28593b4a Mon Sep 17 00:00:00 2001
From: Julien Malka <julien@malka.sh>
Date: Sat, 8 Apr 2023 22:55:19 +0200
Subject: [PATCH] feat: better handling of nixos version depending on the
 machine

---
 flake.nix                                     |  17 ++-
 lib/default.nix                               |   2 +-
 lib/luj.nix                                   |  12 +-
 packages/buildbot/default.nix                 | 138 ++++++++++++++++++
 .../buildbot/skip_test_linux_distro.patch     |  11 ++
 5 files changed, 172 insertions(+), 8 deletions(-)
 create mode 100644 packages/buildbot/default.nix
 create mode 100644 packages/buildbot/skip_test_linux_distro.patch

diff --git a/flake.nix b/flake.nix
index fbf14d0..00356ea 100644
--- a/flake.nix
+++ b/flake.nix
@@ -62,6 +62,13 @@
 
   outputs = { self, nixpkgs, deploy-rs, ... }@inputs:
     let
+      remotePatches = [
+        {
+          meta.description = "pkgs: fix buildbot";
+          url = "https://github.com/NixOS/nixpkgs/pull/142273.diff";
+          sha256 = "sha256-ZCDQ7SpGhH8JvAwWzdcyrc68RFEWHxxAj0M2+AvEzIg=";
+        }
+      ];
       lib = nixpkgs.lib.extend (import ./lib inputs);
       machines_plats = lib.mapAttrsToList (name: value: value.arch) lib.luj.machines;
 
@@ -80,7 +87,15 @@
         })
         (builtins.attrNames (builtins.readDir ./modules)));
 
-      nixosConfigurations = builtins.mapAttrs (name: value: (lib.mkMachine { host = name; host-config = value; modules = self.nixosModules; nixpkgs = inputs.nixos-apple-silicon.inputs.nixpkgs; system = lib.luj.machines.${name}.arch; })) (lib.importConfig ./machines);
+      nixosConfigurations = builtins.mapAttrs
+        (name: value: (lib.mkMachine {
+          host = name;
+          host-config = value;
+          modules = self.nixosModules;
+          nixpkgs = lib.luj.machines.${name}.nixpkgs_version;
+          system = lib.luj.machines.${name}.arch;
+        }))
+        (lib.importConfig ./machines);
 
       deploy.nodes.lambda = {
         hostname = "lambda.julienmalka.me";
diff --git a/lib/default.nix b/lib/default.nix
index f04e742..501b584 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -90,7 +90,7 @@ in
 
 
 
-  luj = import ./luj.nix final;
+  luj = import ./luj.nix inputs final;
 
 }
 
diff --git a/lib/luj.nix b/lib/luj.nix
index babe472..c8c50e0 100644
--- a/lib/luj.nix
+++ b/lib/luj.nix
@@ -1,4 +1,4 @@
-lib: with lib; let
+inputs: lib: with lib; let
   modules = [
     {
       options.machines = mkOption {
@@ -26,23 +26,23 @@ lib: with lib; let
         machines = {
           lisa = {
             arch = "x86_64-linux";
-            nixpkgs_version = "nixpkgs";
+            nixpkgs_version = inputs.nixpkgs;
           };
           newton = {
             arch = "x86_64-linux";
-            nixpkgs_version = "nixpkgs";
+            nixpkgs_version = inputs.nixpkgs;
           };
           macintosh = {
             arch = "aarch64-linux";
-            nixpkgs_version = "nixos-apple-silicon.inputs";
+            nixpkgs_version = inputs.nixos-apple-silicon.inputs.nixpkgs;
           };
           lambda = {
             arch = "aarch64-linux";
-            nixpkgs_version = "nixpkgs";
+            nixpkgs_version = inputs.nixpkgs;
           };
           tower = {
             arch = "x86_64-linux";
-            nixpkgs_version = "nixpkgs";
+            nixpkgs_version = inputs.nixpkgs;
           };
 
         };
diff --git a/packages/buildbot/default.nix b/packages/buildbot/default.nix
new file mode 100644
index 0000000..95d743d
--- /dev/null
+++ b/packages/buildbot/default.nix
@@ -0,0 +1,138 @@
+{ lib
+, stdenv
+, makeWrapper
+, python3
+, git
+, openssh
+, glibcLocales
+, nixosTests
+}:
+
+let
+  python = python3.override {
+    packageOverrides = (self: super: {
+      sqlalchemy = super.sqlalchemy.overridePythonAttrs (old: rec {
+        version = "1.4.47";
+        src = self.fetchPypi {
+          pname = "SQLAlchemy";
+          inherit version;
+          hash = "sha256-lfwC9/wfMZmqpHqKdXQ3E0z2GOnZlMhO/9U/Uww4WG8=";
+        };
+      });
+    });
+  };
+  withPlugins = plugins: python.pkgs.buildPythonPackage {
+    pname = "${package.pname}-with-plugins";
+    inherit (package) version;
+    format = "other";
+
+    dontUnpack = true;
+    dontBuild = true;
+    doCheck = false;
+
+    nativeBuildInputs = [
+      makeWrapper
+    ];
+
+    propagatedBuildInputs = plugins ++ package.propagatedBuildInputs;
+
+    installPhase = ''
+      makeWrapper ${package}/bin/buildbot $out/bin/buildbot \
+        --prefix PYTHONPATH : "${package}/${python.sitePackages}:$PYTHONPATH"
+      ln -sfv ${package}/lib $out/lib
+    '';
+
+    passthru = package.passthru // {
+      withPlugins = morePlugins: withPlugins (morePlugins ++ plugins);
+    };
+  };
+
+  package = python.pkgs.buildPythonPackage rec {
+    pname = "buildbot";
+    version = "3.7.0";
+    format = "setuptools";
+
+
+    src = python.pkgs.fetchPypi {
+      inherit pname version;
+      hash = "sha256-YMLT1SP6NenJIUVTvr58GVrtNXHw+bhfgMpZu3revG4=";
+    };
+
+    propagatedBuildInputs = with python.pkgs; [
+      # core
+      twisted
+      jinja2
+      msgpack
+      zope_interface
+      sqlalchemy
+      alembic
+      python-dateutil
+      txaio
+      autobahn
+      pyjwt
+      pyyaml
+    ]
+    # tls
+    ++ twisted.optional-dependencies.tls;
+
+    nativeCheckInputs = with python.pkgs; [
+      treq
+      txrequests
+      pypugjs
+      boto3
+      moto
+      mock
+      lz4
+      setuptoolsTrial
+      buildbot-worker
+      buildbot-pkg
+      buildbot-plugins.www
+      parameterized
+      git
+      openssh
+      glibcLocales
+    ];
+
+    patches = [
+      # This patch disables the test that tries to read /etc/os-release which
+      # is not accessible in sandboxed builds.
+      ./skip_test_linux_distro.patch
+    ];
+
+    postPatch = ''
+      substituteInPlace buildbot/scripts/logwatcher.py --replace '/usr/bin/tail' "$(type -P tail)"
+    '';
+
+    # Silence the depreciation warning from SqlAlchemy
+    SQLALCHEMY_SILENCE_UBER_WARNING = 1;
+
+    # TimeoutErrors on slow machines -> aarch64
+    doCheck = !stdenv.isAarch64;
+
+    preCheck = ''
+      export LC_ALL="en_US.UTF-8"
+      export PATH="$out/bin:$PATH"
+
+      # remove testfile which is missing configuration file from sdist
+      rm buildbot/test/integration/test_graphql.py
+      # tests in this file are flaky, see https://github.com/buildbot/buildbot/issues/6776
+      rm buildbot/test/integration/test_try_client.py
+    '';
+
+    passthru = {
+      inherit withPlugins;
+      tests.buildbot = nixosTests.buildbot;
+      updateScript = ./update.sh;
+    };
+
+    meta = with lib; {
+      description = "An open-source continuous integration framework for automating software build, test, and release processes";
+      homepage = "https://buildbot.net/";
+      changelog = "https://github.com/buildbot/buildbot/releases/tag/v${version}";
+      maintainers = with maintainers; [ ryansydnor lopsided98 ];
+      license = licenses.gpl2Only;
+      broken = stdenv.isDarwin;
+    };
+  };
+in
+package
diff --git a/packages/buildbot/skip_test_linux_distro.patch b/packages/buildbot/skip_test_linux_distro.patch
new file mode 100644
index 0000000..8fe5c7b
--- /dev/null
+++ b/packages/buildbot/skip_test_linux_distro.patch
@@ -0,0 +1,11 @@
+diff -Nur buildbot-0.9.6/buildbot/test/unit/test_buildbot_net_usage_data.py buildbot-0.9.6.patched/buildbot/test/unit/test_buildbot_net_usage_data.py
+--- buildbot-0.9.6/buildbot/test/unit/test_buildbot_net_usage_data.py	2017-04-19 16:57:02.000000000 +0200
++++ buildbot-0.9.6.patched/buildbot/test/unit/test_buildbot_net_usage_data.py	2017-05-04 12:22:54.575762551 +0200
+@@ -147,6 +147,7 @@
+         _sendBuildbotNetUsageData({'foo': 'bar'})
+ 
+     def test_linux_distro(self):
++        raise SkipTest("NixOS sandboxed builds hides /etc/os-release")
+         system = platform.system()
+         if system != "Linux":
+             raise SkipTest("test is only for linux")