Initial commit

This commit is contained in:
Bouke van der Bijl 2022-12-20 14:18:29 +01:00
commit b2afa2da8c
6 changed files with 199 additions and 0 deletions

7
flake.lock generated Normal file
View File

@ -0,0 +1,7 @@
{
"nodes": {
"root": {}
},
"root": "root",
"version": 7
}

21
flake.nix Normal file
View File

@ -0,0 +1,21 @@
{
description = "Wrapper for arduino-cli";
outputs = { self }: {
mkArduinoPackageOverlay = packageIndexFile: (self: super: {
arduinoPackages = self.lib.recursiveUpdate (super.arduinoPackages or {}) (self.callPackage ./packages.nix {
packageIndex = builtins.fromJSON (builtins.readFile packageIndexFile);
});
});
mkArduinoLibraryOverlay = libraryIndexFile: (self: super: {
arduinoLibraries = self.lib.recursiveUpdate (super.arduinoLibraries or {}) (self.callPackage ./libraries.nix {
libraryIndex = builtins.fromJSON (builtins.readFile libraryIndexFile);
});
});
overlay = (self: super: {
wrapArduinoCLI = self.callPackage ./wrap-arduino-cli.nix { };
});
};
}

32
lib.nix Normal file
View File

@ -0,0 +1,32 @@
{ lib }:
let
alt = a: b: if a == null then b else a;
in
with builtins; rec {
latestVersion = attrs: (builtins.head (builtins.sort (a: b: (builtins.compareVersions a.version b.version) == 1) (builtins.attrValues (builtins.mapAttrs (version: value: { inherit version value; }) attrs)))).value;
selectSystem = system: systems:
if system == "aarch64-darwin" then
alt (lib.findFirst ({host, ...}: (match "arm64-apple-darwin.*" host) != null) null systems) (selectSystem "x86_64-darwin" systems)
else if system == "x86_64-darwin" then
alt (lib.findFirst ({host, ...}: (match "x86_64-apple-darwin.*" host) != null) null systems) (selectSystem "i686-darwin" systems)
else if system == "i686-darwin" then
lib.findFirst ({host, ...}: (match "i[3456]86-apple-darwin.*" host) != null) null systems
else if system == "aarch64-linux" then
lib.findFirst ({host, ...}: (match "(aarch64|arm64)-linux-gnu" host) != null) null systems
else if system == "x86_64-linux" then
lib.findFirst ({host, ...}: (match "x86_64-.*linux-gnu" host) != null) null systems
else null;
convertHash = hash: let
m = (match "(SHA-256|SHA-1|MD5):(.*)" hash);
algo = elemAt m 0;
h = elemAt m 1;
in
if m == null then
throw "Unsupported hash format ${hash}"
else if algo == "SHA-256" then
{ sha256 = h; }
else if algo == "SHA-1" then
{ sha1 = h; }
else
{ md5 = h; };
}

24
libraries.nix Normal file
View File

@ -0,0 +1,24 @@
{ fetchzip, stdenv, lib, libraryIndex, pkgsBuildHost, pkgs, arduinoPackages }:
with builtins;
let
inherit (pkgs.callPackage ./lib.nix {}) convertHash;
libraries = mapAttrs (name: versions: listToAttrs (map ({version, url, checksum, ...}: {
name = version;
value = stdenv.mkDerivation {
pname = name;
inherit version;
installPhase = ''
mkdir -p "$out/libraries/$pname"
cp -R * "$out/libraries/$pname/"
'';
nativeBuildInputs = [ pkgs.unzip ];
src = fetchurl ({
url = url;
} // (convertHash checksum));
};
}) versions)) (groupBy ({ name, ... }: name) libraryIndex.libraries);
in
libraries

80
packages.nix Normal file
View File

@ -0,0 +1,80 @@
# From tools.go in arduino-cli
# regexpLinuxArm = regexp.MustCompile("arm.*-linux-gnueabihf")
# regexpLinuxArm64 = regexp.MustCompile("(aarch64|arm64)-linux-gnu")
# regexpLinux64 = regexp.MustCompile("x86_64-.*linux-gnu")
# regexpLinux32 = regexp.MustCompile("i[3456]86-.*linux-gnu")
# regexpWindows32 = regexp.MustCompile("i[3456]86-.*(mingw32|cygwin)")
# regexpWindows64 = regexp.MustCompile("(amd64|x86_64)-.*(mingw32|cygwin)")
# regexpMac64 = regexp.MustCompile("x86_64-apple-darwin.*")
# regexpMac32 = regexp.MustCompile("i[3456]86-apple-darwin.*")
# regexpMacArm64 = regexp.MustCompile("arm64-apple-darwin.*")
# regexpFreeBSDArm = regexp.MustCompile("arm.*-freebsd[0-9]*")
# regexpFreeBSD32 = regexp.MustCompile("i?[3456]86-freebsd[0-9]*")
# regexpFreeBSD64 = regexp.MustCompile("amd64-freebsd[0-9]*")
{ fetchzip, stdenv, lib, packageIndex, pkgsBuildHost, pkgs, arduinoPackages }:
with builtins;
let
inherit (pkgsBuildHost.xorg) lndir;
inherit (pkgs.callPackage ./lib.nix {}) selectSystem convertHash;
# Tools are installed in $platform_name/tools/$name/$version
tools = listToAttrs (map ({ name, tools, ... }: {
inherit name;
value = let platformName = name; in mapAttrs (_: versions: listToAttrs (map ({name, version, systems, ...}: {
name = version;
value = let
system = selectSystem stdenv.hostPlatform.system systems;
in
if system == null then
throw "Unsupported platform ${stdenv.hostPlatform.system}"
else
stdenv.mkDerivation {
pname = "${platformName}-${name}";
inherit version;
dirName = "packages/${platformName}/tools/${name}/${version}";
installPhase = ''
mkdir -p "$out/$dirName"
cp -R * "$out/$dirName/"
'';
nativeBuildInputs = [ pkgs.unzip ];
src = fetchurl ({
url = system.url;
} // (convertHash system.checksum));
};
}) versions)) (groupBy ({ name, ... }: name) tools);
}) packageIndex.packages);
# Platform are installed in $platform_name/hardware/$architecture/$version
platforms = listToAttrs (map ({ name, platforms, ... }: {
inherit name;
value = mapAttrs (architecture: versions: listToAttrs (map ({version, url, checksum, toolsDependencies ? [], ...}: {
name = version;
value = stdenv.mkDerivation {
pname = "${name}-${architecture}";
inherit version;
dirName = "packages/${name}/hardware/${architecture}/${version}";
toolsDependencies = map ({packager, name, version}: arduinoPackages.tools.${packager}.${name}.${version}) toolsDependencies;
passAsFile = [ "toolsDependencies" ];
installPhase = ''
mkdir -p "$out/$dirName"
cp -R * "$out/$dirName/"
for i in $(cat $toolsDependenciesPath); do
${lndir}/bin/lndir -silent $i $out
done
'';
nativeBuildInputs = [ pkgs.unzip ];
src = fetchurl ({
url = url;
} // (convertHash checksum));
};
}) versions)) (groupBy ({ architecture, ... }: architecture) platforms);
}) packageIndex.packages);
in
{
inherit tools platforms;
}

35
wrap-arduino-cli.nix Normal file
View File

@ -0,0 +1,35 @@
{ lib, pkgs }:
let
wrap = {
packages ? []
, libraries ? []
}:
let
inherit (pkgs.callPackage ./lib.nix {}) latestVersion;
builtinPackages = (map latestVersion (builtins.attrValues pkgs.arduinoPackages.tools.builtin));
libPath = pkgs.symlinkJoin {
name = "arduino-libraries";
paths = libraries;
};
dataPath = pkgs.symlinkJoin {
name = "arduino-data";
paths = builtinPackages ++ packages ++ [
# Add some dummy files to keep the CLI happy
(pkgs.writeTextDir "inventory.yaml" (builtins.toJSON {}))
(pkgs.writeTextDir "package_index.json" (builtins.toJSON {packages = [];}))
(pkgs.writeTextDir "library_index.json" (builtins.toJSON {libraries = [];}))
];
};
in
pkgs.runCommand "arduino-cli-wrapped" {
buildInputs = [ pkgs.makeWrapper ];
meta.mainProgram = "arduino-cli";
} ''
makeWrapper ${pkgs.arduino-cli}/bin/arduino-cli $out/bin/arduino-cli --set ARDUINO_UPDATER_ENABLE_NOTIFICATION false --set ARDUINO_DIRECTORIES_DATA ${dataPath} --set ARDUINO_DIRECTORIES_USER ${libPath}
'';
in
lib.makeOverridable wrap