Skip to content
Snippets Groups Projects
default.nix 2.45 KiB
Newer Older
Legy (Beini)'s avatar
Legy (Beini) committed
{ pkgs ? import <nixpkgs> {} }:

let

Legy (Beini)'s avatar
Legy (Beini) committed
  python3' = let
Legy (Beini)'s avatar
Legy (Beini) committed
    packageOverrides = self: super: {
      pyparsing = super.pyparsing.overridePythonAttrs( old: rec {
        version = "2.3.1";
        src = super.fetchPypi {
          pname="pyparsing";
          inherit version;
          sha256="66c9268862641abcac4a96ba74506e594c884e3f57690a696d21ad8210ed667a";
        };
      });
    };
Legy (Beini)'s avatar
Legy (Beini) committed
    in pkgs.python3.override{inherit packageOverrides;};

  python3RequiredPackages = p: with p; [
    p.setuptools
    p.click
    p.pyserial
    p.future
    p.cryptography
    p.pyparsing
    p.pyelftools
  ];
Legy (Beini)'s avatar
Legy (Beini) committed

  qthingDependencies = with pkgs; [
Legy (Beini)'s avatar
Legy (Beini) committed
    cmake (python3'.withPackages python3RequiredPackages) git ncurses flex bison gperf ninja
Legy (Beini)'s avatar
Legy (Beini) committed
    (pkgs.callPackage ./esp32-toolchain.nix {})
  ];

  shellSetup = ''
    export NIX_CFLAGS_LINK=-lncurses
Legy (Beini)'s avatar
Legy (Beini) committed
    export IDF_PATH=$(realpath ./esp-idf)
Legy (Beini)'s avatar
Legy (Beini) committed
    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
    for file in *; do
      [[ "$file" == environment.h ]] && continue
      ln -s ${./.}/qthing/$file $out/qthing/$file
Legy (Beini)'s avatar
Legy (Beini) committed
in rec {

  shell = pkgs.stdenv.mkDerivation {
    name = "esp-idf-env";
    buildInputs = qthingDependencies;
    shellHook = shellSetup;
  };

  mkProject = { name, src, environment }: let
Legy (Beini)'s avatar
Legy (Beini) committed
    flashScript = pkgs.writeScript "flash" ''
      PATH=${pkgs.esptool}/bin/:$PATH
      cd @out@
      esptool.py "$@" write_flash @flash_cmdline@
    '';
  in pkgs.stdenv.mkDerivation {
    inherit name qthingBaseRepo;
Legy (Beini)'s avatar
Legy (Beini) committed
    buildInputs = qthingDependencies;
    unpackPhase = ''
      cp --reflink=auto -r ${qthingBaseRepo}/* ./
      chmod u+w qthing
      cp --reflink=auto -r ${src} ./main
      cp --reflink=auto ${environment} ./qthing/environment.h
Legy (Beini)'s avatar
Legy (Beini) committed
    configurePhase = shellSetup + ''
      patchShebangs esp-idf
      cmake -G Ninja -S . -B build
Legy (Beini)'s avatar
Legy (Beini) committed
    '';
    buildPhase = ''
      ninja -C build -j $NIX_BUILD_CORES
      flash_cmdline=$(cat build/flash_project_args | tr '\n' ' ') substituteAll ${flashScript} build/flash
Legy (Beini)'s avatar
Legy (Beini) committed
      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
Legy (Beini)'s avatar
Legy (Beini) committed
      done
      cp build/flash $out/bin
Legy (Beini)'s avatar
Legy (Beini) committed
    '';
  };

}