Skip to content
Snippets Groups Projects
Commit c01bf615 authored by fpletz's avatar fpletz :snowflake: Committed by dx
Browse files

add nix flake & direnv support

parent c2d40d28
Branches
No related tags found
1 merge request!176add nix flake & direnv support
.envrc 0 → 100644
use flake
...@@ -22,3 +22,9 @@ __pycache__ ...@@ -22,3 +22,9 @@ __pycache__
docs/_build/ docs/_build/
/tools/mypy.sh /tools/mypy.sh
# direnv
.direnv
# nix
result*
...@@ -34,7 +34,7 @@ If you've already cloned without ``--recursive`` you can update your submodules ...@@ -34,7 +34,7 @@ If you've already cloned without ``--recursive`` you can update your submodules
Dependencies Dependencies
------------ ------------
If you're using Nix(OS), just run ``nix-shell nix/shell.nix``. If you're using Nix(OS), just run ``nix-shell nix/shell.nix``. There is also a flake available for ``nix develop``.
On other Linux-based distributions, you will have to manually install ESP-IDF alongside our custom patches (note that install.sh installs stuff to your $HOME so that you may want to use a container or nix): On other Linux-based distributions, you will have to manually install ESP-IDF alongside our custom patches (note that install.sh installs stuff to your $HOME so that you may want to use a container or nix):
......
{
"nodes": {
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1673956053,
"narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1692128808,
"narHash": "sha256-Di1Zm/P042NuwThMiZNrtmaAjd4Tm2qBOKHX7xUOfMk=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "4ed9856be002a730234a1a1ed9dcd9dd10cbdb40",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-compat": "flake-compat",
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}
flake.nix 0 → 100644
{
description = "flow3r badge flake";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
};
outputs =
{ self
, nixpkgs
, ...
}:
let
supportedSystems = [
/* FIXME "aarch64-linux" */
"x86_64-linux"
];
pkgsFor = system:
nixpkgs.legacyPackages.${system}.extend self.overlays.default;
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
forAllPkgs = f: forAllSystems (system: f (pkgsFor system));
# All packages require to build/lint the project.
fwbuild = pkgs: with pkgs; [
gcc-xtensa-esp32s3-elf-bin
esp-idf
esp-llvm
esptool
run-clang-tidy
git
wget
gnumake
flex
bison
gperf
pkgconfig
gnutar
curl
bzip2
gcc
gnused
findutils
gnugrep
cmake
ninja
python3Packages.sphinx
python3Packages.sphinx_rtd_theme
python3Packages.black
mypy
];
fwdev = pkgs: (fwbuild pkgs) ++ (with pkgs; [
openocd-esp32-bin
python3Packages.pygame
python3Packages.wasmer
python3Packages.wasmer-compiler-cranelift
emscripten
ncurses5
esp-gdb
mpremote
]);
in
{
overlays.default = import ./nix/overlay;
packages = forAllPkgs (pkgs:
{
dockerImage = pkgs.dockerTools.buildImage {
name = "flow3r-build";
copyToRoot = pkgs.buildEnv {
name = "flow3r-build-root";
paths = with pkgs; [
# interactive shell
bashInteractive
coreutils-full
cacert
] ++ fwbuild pkgs;
pathsToLink = [ "/bin" ];
};
runAsRoot = ''
#!${pkgs.runtimeShell}
mkdir -p /tmp
'';
config = {
Env = [
"PATH=/bin:${pkgs.esp-idf}/tools"
"PYTHONPATH=${pkgs.python3.pkgs.makePythonPath pkgs.esp-idf.propagatedBuildInputs}"
"IDF_PATH=${pkgs.esp-idf}"
"IDF_COMPONENT_MANAGER=0"
"TMPDIR=/tmp"
"NIX_SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"
];
};
};
});
devShells = forAllPkgs (pkgs:
{
default = pkgs.mkShell {
name = "flow3r-shell";
buildInputs = (fwdev pkgs) ++ (with pkgs; [
micropython
]);
shellHook = ''
# For esp.py openocd integration.
export OPENOCD_SCRIPTS="${pkgs.openocd-esp32-bin}/share/openocd/scripts"
# Some nice-to-have defaults.
export ESPPORT=/dev/ttyACM0
export OPENOCD_COMMANDS="-f board/esp32s3-builtin.cfg"
# openocd wants udev
export LD_LIBRARY_PATH="${pkgs.systemd}/lib:$LD_LIBRARY_PATH"
'';
};
});
};
}
(import ./flake-compat.nix).defaultNix
with import ./pkgs.nix;
pkgs.dockerTools.buildImage {
name = "registry.gitlab.com/flow3r-badge/flow3r-build";
copyToRoot = pkgs.buildEnv {
name = "image-root";
paths = with pkgs; [
# interactive shell
bashInteractive
coreutils-full
# esp crud
esp-idf
esptool
run-clang-tidy
gcc-xtensa-esp32s3-elf-bin
# esp-llvm goes into PATH because it has conflicting binary names.
mypy
(python3.withPackages (ps: with ps; [
sphinx sphinx_rtd_theme
black
# simulator deps
pygame wasmer
wasmer-compiler-cranelift
pymad requests
]))
# random build tools
gcc gnused findutils gnugrep
git wget gnumake
cmake ninja pkgconfig
gnutar curl bzip2
cacert
];
pathsToLink = [ "/bin" ];
};
runAsRoot = ''
#!${pkgs.runtimeShell}
mkdir -p /tmp
'';
config = {
Env = [
"PATH=/bin:${pkgs.esp-idf}/tools:${pkgs.esp-llvm}/bin"
"PYTHONPATH=${pkgs.python3.pkgs.makePythonPath pkgs.esp-idf.propagatedBuildInputs}"
"IDF_PATH=${pkgs.esp-idf}"
"IDF_COMPONENT_MANAGER=0"
"TMPDIR=/tmp"
"NIX_SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"
];
};
}
(import
(
let lock = builtins.fromJSON (builtins.readFile ../flake.lock); in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{ src = ../.; }
)
(self: super: { (final: prev: {
gcc-xtensa-esp32s3-elf-bin = super.callPackage ./esp32s3-toolchain-bin.nix {}; gcc-xtensa-esp32s3-elf-bin = final.callPackage ./esp32s3-toolchain-bin.nix {};
openocd-esp32-bin = super.callPackage ./openocd-esp32-bin.nix {}; openocd-esp32-bin = final.callPackage ./openocd-esp32-bin.nix {};
esp-idf = super.callPackage ./esp-idf {}; esp-idf = final.callPackage ./esp-idf {};
esp-llvm = super.callPackage ./esp-llvm.nix {}; esp-llvm = final.callPackage ./esp-llvm.nix {};
esp-gdb = super.callPackage ./esp-gdb.nix {}; esp-gdb = final.callPackage ./esp-gdb.nix {};
run-clang-tidy = super.callPackage ./run-clang-tidy {}; run-clang-tidy = final.callPackage ./run-clang-tidy {};
mpremote = super.python310Packages.callPackage ./mpremote {}; mpremote = final.python310Packages.callPackage ./mpremote {};
}) })
...@@ -45,6 +45,7 @@ stdenv.mkDerivation rec { ...@@ -45,6 +45,7 @@ stdenv.mkDerivation rec {
description = "Espressif LLVM/Clang fork binary build"; description = "Espressif LLVM/Clang fork binary build";
homepage = "https://docs.espressif.com/projects/esp-idf/en/stable/get-started/linux-setup.html"; homepage = "https://docs.espressif.com/projects/esp-idf/en/stable/get-started/linux-setup.html";
license = licenses.gpl3; license = licenses.gpl3;
priority = -23;
}; };
} }
let
sources = import ./sources.nix;
nixpkgs = import sources.nixpkgs {
overlays = [
(import ./overlay)
];
};
in with nixpkgs; rec {
# nixpkgs passthrough
inherit (nixpkgs) pkgs lib;
# All packages require to build/lint the project.
fwbuild = [
gcc-xtensa-esp32s3-elf-bin
esp-idf
esp-llvm
esptool
run-clang-tidy
git wget gnumake
flex bison gperf pkgconfig
cmake ninja
python3Packages.sphinx
python3Packages.sphinx_rtd_theme
python3Packages.black
mypy
];
fwdev = fwbuild ++ [
openocd-esp32-bin
python3Packages.pygame
python3Packages.wasmer
python3Packages.wasmer-compiler-cranelift
python3Packages.pymad
python3Packages.requests
emscripten
ncurses5
esp-gdb
mpremote
];
}
with import ./pkgs.nix; (import ./flake-compat.nix).shellNix
pkgs.mkShell {
name = "flow3r-shell";
buildInputs = fwdev;
shellHook = ''
# For esp.py openocd integration.
export OPENOCD_SCRIPTS="${pkgs.openocd-esp32-bin}/share/openocd/scripts"
# Some nice-to-have defaults.
export ESPPORT=/dev/ttyACM0
export OPENOCD_COMMANDS="-f board/esp32s3-builtin.cfg"
# openocd wants udev
export LD_LIBRARY_PATH="${pkgs.systemd}/lib:$LD_LIBRARY_PATH"
'';
}
{
"nixpkgs": {
"branch": "master",
"description": "Nix Packages collection",
"homepage": "",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "68b3d3225096da1bc66ba8e65f69fa8d19248358",
"sha256": "0ccljssfq473pf5dhy1zcf7gb2adapgj9jcbkk1mmn9g025533wn",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/68b3d3225096da1bc66ba8e65f69fa8d19248358.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
}
}
# This file has been generated by Niv.
let
#
# The fetchers. fetch_<type> fetches specs of type <type>.
#
fetch_file = pkgs: name: spec:
let
name' = sanitizeName name + "-src";
in
if spec.builtin or true then
builtins_fetchurl { inherit (spec) url sha256; name = name'; }
else
pkgs.fetchurl { inherit (spec) url sha256; name = name'; };
fetch_tarball = pkgs: name: spec:
let
name' = sanitizeName name + "-src";
in
if spec.builtin or true then
builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
else
pkgs.fetchzip { name = name'; inherit (spec) url sha256; };
fetch_git = name: spec:
let
ref =
spec.ref or (
if spec ? branch then "refs/heads/${spec.branch}" else
if spec ? tag then "refs/tags/${spec.tag}" else
abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!"
);
submodules = spec.submodules or false;
submoduleArg =
let
nixSupportsSubmodules = builtins.compareVersions builtins.nixVersion "2.4" >= 0;
emptyArgWithWarning =
if submodules
then
builtins.trace
(
"The niv input \"${name}\" uses submodules "
+ "but your nix's (${builtins.nixVersion}) builtins.fetchGit "
+ "does not support them"
)
{ }
else { };
in
if nixSupportsSubmodules
then { inherit submodules; }
else emptyArgWithWarning;
in
builtins.fetchGit
({ url = spec.repo; inherit (spec) rev; inherit ref; } // submoduleArg);
fetch_local = spec: spec.path;
fetch_builtin-tarball = name: throw
''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`.
$ niv modify ${name} -a type=tarball -a builtin=true'';
fetch_builtin-url = name: throw
''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`.
$ niv modify ${name} -a type=file -a builtin=true'';
#
# Various helpers
#
# https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695
sanitizeName = name:
(
concatMapStrings (s: if builtins.isList s then "-" else s)
(
builtins.split "[^[:alnum:]+._?=-]+"
((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name)
)
);
# The set of packages used when specs are fetched using non-builtins.
mkPkgs = sources: system:
let
sourcesNixpkgs =
import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; };
hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
hasThisAsNixpkgsPath = <nixpkgs> == ./.;
in
if builtins.hasAttr "nixpkgs" sources
then sourcesNixpkgs
else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
import <nixpkgs> { }
else
abort
''
Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
add a package called "nixpkgs" to your sources.json.
'';
# The actual fetching function.
fetch = pkgs: name: spec:
if ! builtins.hasAttr "type" spec then
abort "ERROR: niv spec ${name} does not have a 'type' attribute"
else if spec.type == "file" then fetch_file pkgs name spec
else if spec.type == "tarball" then fetch_tarball pkgs name spec
else if spec.type == "git" then fetch_git name spec
else if spec.type == "local" then fetch_local spec
else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
else if spec.type == "builtin-url" then fetch_builtin-url name
else
abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
# If the environment variable NIV_OVERRIDE_${name} is set, then use
# the path directly as opposed to the fetched source.
replace = name: drv:
let
saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name;
ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
in
if ersatz == "" then drv else
# this turns the string into an actual Nix path (for both absolute and
# relative paths)
if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}";
# Ports of functions for older nix versions
# a Nix version of mapAttrs if the built-in doesn't exist
mapAttrs = builtins.mapAttrs or (
f: set: with builtins;
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
);
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
range = first: last: if first > last then [ ] else builtins.genList (n: first + n) (last - first + 1);
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
concatMapStrings = f: list: concatStrings (map f list);
concatStrings = builtins.concatStringsSep "";
# https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331
optionalAttrs = cond: as: if cond then as else { };
# fetchTarball version that is compatible between all the versions of Nix
builtins_fetchTarball = { url, name ? null, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchTarball;
in
if lessThan nixVersion "1.12" then
fetchTarball ({ inherit url; } // (optionalAttrs (name != null) { inherit name; }))
else
fetchTarball attrs;
# fetchurl version that is compatible between all the versions of Nix
builtins_fetchurl = { url, name ? null, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchurl;
in
if lessThan nixVersion "1.12" then
fetchurl ({ inherit url; } // (optionalAttrs (name != null) { inherit name; }))
else
fetchurl attrs;
# Create the final "sources" from the config
mkSources = config:
mapAttrs
(
name: spec:
if builtins.hasAttr "outPath" spec
then
abort
"The values in sources.json should not have an 'outPath' attribute"
else
spec // { outPath = replace name (fetch config.pkgs name spec); }
)
config.sources;
# The "config" used by the fetchers
mkConfig =
{ sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null
, sources ? if sourcesFile == null then { } else builtins.fromJSON (builtins.readFile sourcesFile)
, system ? builtins.currentSystem
, pkgs ? mkPkgs sources system
}: rec {
# The sources, i.e. the attribute set of spec name to spec
inherit sources;
# The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
inherit pkgs;
};
in
mkSources (mkConfig { }) // { __functor = _: settings: mkSources (mkConfig settings); }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment