{ pkgs ? import <nixpkgs> {} }: let python3' = let packageOverrides = self: super: { pyparsing = super.pyparsing.overridePythonAttrs( old: rec { version = "2.3.1"; src = super.fetchPypi { pname="pyparsing"; inherit version; sha256="66c9268862641abcac4a96ba74506e594c884e3f57690a696d21ad8210ed667a"; }; }); }; in pkgs.python3.override{inherit packageOverrides;}; python3RequiredPackages = p: with p; [ p.setuptools p.click p.pyserial p.future p.cryptography p.pyparsing p.pyelftools ]; qthingDependencies = with pkgs; [ cmake (python3'.withPackages python3RequiredPackages) git ncurses flex bison gperf ninja (pkgs.callPackage ./esp32-toolchain.nix {}) ]; shellSetup = '' export NIX_CFLAGS_LINK=-lncurses export IDF_PATH=$(realpath ./esp-idf) export IDF_TOOLS_PATH=$IDF_PATH/tools ''; qthingBaseRepo = pkgs.runCommand "qthing" {} '' mkdir -p $out/qthing for file in CMakeLists.txt components esp-idf sdkconfig; do ln -s ${./.}/$file $out/$file done cd ${./.}/qthing for file in *; do [[ "$file" == environment.h ]] && continue ln -s ${./.}/qthing/$file $out/qthing/$file done ''; in rec { shell = pkgs.stdenv.mkDerivation { name = "esp-idf-env"; buildInputs = qthingDependencies; shellHook = shellSetup; }; mkProject = { name, src, environment }: let flashScript = pkgs.writeScript "flash" '' PATH=${pkgs.esptool}/bin/:$PATH cd @out@ esptool.py "$@" write_flash @flash_cmdline@ ''; defaultDeviceConfig = pkgs.writeText "device_config.h" '' #define DEVICE_NAME "${name}" ''; defaultCMakeLists = pkgs.writeText "CMakeLists.txt" '' idf_component_register(SRC_DIRS "." INCLUDE_DIRS "." REQUIRES main) ''; in pkgs.stdenv.mkDerivation { inherit name qthingBaseRepo; buildInputs = qthingDependencies; unpackPhase = '' cp --reflink=auto -r ${qthingBaseRepo}/* ./ cp --reflink=auto -r ${src} ./main chmod u+w ./qthing ./main if [[ ! -f main/device_config.h ]]; then ln -s ${defaultDeviceConfig} main/device_config.h fi if [[ ! -f main/CMakeLists.txt ]]; then ln -s ${defaultCMakeLists} main/CMakeLists.txt fi cp --reflink=auto ${environment} ./qthing/environment.h ''; configurePhase = shellSetup + '' patchShebangs esp-idf cmake -G Ninja -S . -B build ''; buildPhase = '' ninja -C build -j $NIX_BUILD_CORES flash_cmdline=$(cat build/flash_project_args | tr '\n' ' ') substituteAll ${flashScript} build/flash chmod +x build/flash ''; installPhase = '' mkdir -p $out/bin for path in $(cd build; find -iname '*.bin' -and -not -wholename '*/CMakeFiles/*'); do mkdir -p $out/$(dirname $path) cp build/$path $out/$path done cp build/flash $out/bin ''; }; }