From aea24e6ea63663401d00ada2d102740b3b295283 Mon Sep 17 00:00:00 2001 From: Hugo Hakim Damer Date: Tue, 16 Sep 2025 23:47:06 +0200 Subject: [PATCH 01/13] chore: add Nix-based development environment + dev container --- .devcontainer.json | 35 ++++++++++ .envrc | 10 +++ .gitignore | 25 +++++++ devenv.lock | 158 ++++++++++++++++++++++++++++++++++++++++++ devenv.nix | 167 +++++++++++++++++++++++++++++++++++++++++++++ devenv.yaml | 15 ++++ 6 files changed, 410 insertions(+) create mode 100644 .devcontainer.json create mode 100644 .envrc create mode 100644 devenv.lock create mode 100644 devenv.nix create mode 100644 devenv.yaml diff --git a/.devcontainer.json b/.devcontainer.json new file mode 100644 index 00000000..11fcc81f --- /dev/null +++ b/.devcontainer.json @@ -0,0 +1,35 @@ +{ + "containerEnv": { + "HOME": "/home/vscode" + }, + "containerUser": "vscode", + "customizations": { + "jetbrains": { + "plugins": [ + "com.jetbrains.rust", + "systems.fehn.intellijdirenv" + ] + }, + "vscode": { + "extensions": [ + "rust-lang.rust-analyzer", + "ms-vscode.cpptools", + "mkhl.direnv", + "vadimcn.vscode-lldb", + "tamasfe.even-better-toml" + ], + "settings": { + "rust-analyzer.cargo.features": [ + "vendored" + ] + } + } + }, + "image": "ghcr.io/cachix/devenv/devcontainer:latest", + "overrideCommand": false, + "runArgs": [ + "--userns=keep-id:uid=1000,gid=1000" + ], + "updateContentCommand": "devenv test", + "updateRemoteUserUID": true +} diff --git a/.envrc b/.envrc new file mode 100644 index 00000000..7e9a2d60 --- /dev/null +++ b/.envrc @@ -0,0 +1,10 @@ +export DIRENV_WARN_TIMEOUT=20s + +eval "$(devenv direnvrc)" + +# `use devenv` supports the same options as the `devenv shell` command. +# +# To silence the output, use `--quiet`. +# +# Example usage: use devenv --quiet --impure --option services.postgres.enable:bool true +use devenv diff --git a/.gitignore b/.gitignore index bc1dc4b5..11b71119 100644 --- a/.gitignore +++ b/.gitignore @@ -87,3 +87,28 @@ fabric.properties # Android studio 3.1+ serialized cache file .idea/caches/build_file_checksums.ser + +# Devenv +.devenv* +devenv.local.nix + +# direnv +.direnv + +# pre-commit +.pre-commit-config.yaml + +# VSCode, see https://github.com/github/gitignore/blob/main/VisualStudio.gitignore +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix diff --git a/devenv.lock b/devenv.lock new file mode 100644 index 00000000..94a7c71a --- /dev/null +++ b/devenv.lock @@ -0,0 +1,158 @@ +{ + "nodes": { + "devenv": { + "locked": { + "dir": "src/modules", + "lastModified": 1757852601, + "owner": "cachix", + "repo": "devenv", + "rev": "8190bcacd6077785bc35b03fb281cc410f74d629", + "type": "github" + }, + "original": { + "dir": "src/modules", + "owner": "cachix", + "repo": "devenv", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1747046372, + "owner": "edolstra", + "repo": "flake-compat", + "rev": "9100a0f413b0c601e0533d1d94ffd501ce2e7885", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "git-hooks": { + "inputs": { + "flake-compat": "flake-compat", + "gitignore": "gitignore", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1757588530, + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "b084b2c2b6bc23e83bbfe583b03664eb0b18c411", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "git-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1709087332, + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "mk-shell-bin": { + "locked": { + "lastModified": 1677004959, + "owner": "rrbutani", + "repo": "nix-mk-shell-bin", + "rev": "ff5d8bd4d68a347be5042e2f16caee391cd75887", + "type": "github" + }, + "original": { + "owner": "rrbutani", + "repo": "nix-mk-shell-bin", + "type": "github" + } + }, + "nix2container": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1752002763, + "owner": "nlewo", + "repo": "nix2container", + "rev": "4f2437f6a1844b843b380d483087ae6d461240ee", + "type": "github" + }, + "original": { + "owner": "nlewo", + "repo": "nix2container", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1755783167, + "owner": "cachix", + "repo": "devenv-nixpkgs", + "rev": "4a880fb247d24fbca57269af672e8f78935b0328", + "type": "github" + }, + "original": { + "owner": "cachix", + "ref": "rolling", + "repo": "devenv-nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "devenv": "devenv", + "git-hooks": "git-hooks", + "mk-shell-bin": "mk-shell-bin", + "nix2container": "nix2container", + "nixpkgs": "nixpkgs", + "pre-commit-hooks": [ + "git-hooks" + ], + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1757817311, + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "7005365b81bcc335f2b744aa30446f4e575e8dc2", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/devenv.nix b/devenv.nix new file mode 100644 index 00000000..b0c84ed5 --- /dev/null +++ b/devenv.nix @@ -0,0 +1,167 @@ +{ pkgs, lib, config, inputs, ... }: +let + possible_dtls_libraries = [ "openssl" "mbedtls" "gnutls" "wolfssl" ]; +in { + # https://devenv.sh/basics/ + options = { + rust-version = lib.mkOption { + type = lib.types.enum [ "msrv" "stable" "nightly" ]; + default = "stable"; + description = "Version of the Rust toolchain to use"; + }; + + dtls-library = lib.mkOption { + type = lib.types.enum ([ "all" "none" ] ++ possible_dtls_libraries); + default = "all"; + description = "DTLS library/libraries that should be provided for libcoap to link against."; + }; + }; + + config = rec { + # https://devenv.sh/packages/ + packages = + let wolfssl-libcoap = (pkgs.wolfssl.override { + variant = "opensslall"; + extraConfigureFlags = [ + "--enable-dtls" + "--enable-psk" + "--enable-aesccm" + "--enable-dh" + ]; + }).overrideAttrs (final: prev: { + # Need to override the CFLAGS in the preConfigure script, because + # adding more than one CFLAG to extraConfigureFlags causes issues + # due to the whitespace (see + # https://discourse.nixos.org/t/cmakeflags-and-spaces-in-option-values/20170) + preConfigure = '' + export CFLAGS="-DHAVE_ALPN -DHAVE_EX_DATA" + ''; + }); + in with pkgs; ( + [ + autoconf automake libtool pkg-config lldb rustPlatform.bindgenHook + ] + ++ (if config.dtls-library == "all" then [ openssl mbedtls gnutls wolfssl-libcoap ] + else if config.dtls-library == "openssl" then [ openssl ] + else if config.dtls-library == "mbedtls" then [ mbedtls ] + else if config.dtls-library == "gnutls" then [ gnutls ] + else if config.dtls-library == "wolfssl" then [ wolfssl-libcoap ] + else []) + ); + + # https://devenv.sh/languages/ + languages = { + rust = let + libcoap-msrv = (builtins.fromTOML (builtins.readFile ./libcoap/Cargo.toml)).package.rust-version; + libcoap-sys-msrv = (builtins.fromTOML (builtins.readFile ./libcoap-sys/Cargo.toml)).package.rust-version; + msrv = if (builtins.compareVersions libcoap-msrv libcoap-sys-msrv) >= 0 then libcoap-msrv else libcoap-sys-msrv; + in { + enable = true; + channel = if config.rust-version == "msrv" then "stable" else config.rust-version; + version = if config.rust-version == "msrv" then msrv else "latest"; + components = ["rustc" "cargo" "clippy" "rustfmt" "rust-analyzer" "rust-src"]; + }; + c = { + enable = true; + }; + }; + + # https://devenv.sh/processes/ + + # https://devenv.sh/services/ + + # https://devenv.sh/scripts/ + scripts = let + default_features = [ "vendored" ]; + crate_specific_feature_matrix = { + libcoap-rs = [ "tcp" "rand" ]; + libcoap-sys = [ "default" ]; + }; + dtls_supported_feature_matrix = { + openssl = [ "tls" "dtls-psk" "dtls-pki" ]; + mbedtls = [ "tls" "dtls-psk" "dtls-pki" ]; + gnutls = [ "tls" "dtls-psk" "dtls-pki" "dtls-rpk" ]; + tinydtls = [ "dtls-psk" "dtls-rpk" "dtls-tinydtls-sys-vendored" ]; + wolfssl = [ "tls" "dtls-psk" "dtls-pki"]; + }; + in builtins.listToAttrs (lib.lists.flatten (lib.mapAttrsToList (dtls_lib: dtls_features: + if config.dtls-library == "all" || dtls_lib == "tinydtls" || config.dtls-library == dtls_lib then + (lib.mapAttrsToList (crate_name: crate_features: + (lib.nameValuePair (crate_name + ":test_" + dtls_lib) { + exec = '' + LIBCOAP_RS_DTLS_BACKEND=${dtls_lib} cargo test -p ${crate_name} --no-default-features --features ${ lib.concatStringsSep "," (default_features ++ crate_features ++ dtls_features) } "$@" + ''; + description = "Run all possible tests for the ${crate_name} crate using the ${dtls_lib} DTLS library."; + }) + ) crate_specific_feature_matrix) + else [] + ) dtls_supported_feature_matrix)); + + enterShell = '' + cat << EOF + + Entering the libcoap-rs development environment! + + Environment-provided DTLS libraries: ${ if config.dtls-library == "all" then lib.concatStringsSep "," possible_dtls_libraries else config.dtls-library} + + Some helpful scripts/commands you can run for your convenience: + ${ lib.concatStringsSep "\n" (lib.mapAttrsToList (name: value: "${name} \t${value.description}") scripts) } + + EOF + ''; + + # https://devenv.sh/tasks/ + # tasks = { + # "myproj:setup".exec = "mytool build"; + # "devenv:enterShell".after = [ "myproj:setup" ]; + # }; + + # https://devenv.sh/tests/ + enterTest = '' + ''; + + # https://devenv.sh/git-hooks/ + # git-hooks.hooks.shellcheck.enable = true; + git-hooks.hooks = { + clippy = { + enable = true; + settings.allFeatures = true; + }; + rustfmt = { + enable = true; + settings.check = true; + }; + }; + + # See full reference at https://devenv.sh/reference/options/ + + devcontainer.enable = true; + devcontainer.settings = { + updateContentCommand = "devenv test"; + runArgs = [ + "--userns=keep-id:uid=1000,gid=1000" + ]; + containerUser = "vscode"; + updateRemoteUserUID = true; + containerEnv = { + HOME = "/home/vscode"; + }; + customizations.jetbrains.plugins = [ + "com.jetbrains.rust" + "systems.fehn.intellijdirenv" + ]; + customizations.vscode = { + extensions = [ + "rust-lang.rust-analyzer" + "ms-vscode.cpptools" + "mkhl.direnv" + "vadimcn.vscode-lldb" + "tamasfe.even-better-toml" + ]; + settings = { + "rust-analyzer.cargo.features" = ["vendored"]; + }; + }; + }; + }; +} diff --git a/devenv.yaml b/devenv.yaml new file mode 100644 index 00000000..290910d1 --- /dev/null +++ b/devenv.yaml @@ -0,0 +1,15 @@ +inputs: + mk-shell-bin: + url: github:rrbutani/nix-mk-shell-bin + nix2container: + url: github:nlewo/nix2container + inputs: + nixpkgs: + follows: nixpkgs + nixpkgs: + url: github:cachix/devenv-nixpkgs/rolling + rust-overlay: + url: github:oxalica/rust-overlay + inputs: + nixpkgs: + follows: nixpkgs From 219e6cfb22c24bcce56d97663d9145292e7788c7 Mon Sep 17 00:00:00 2001 From: Hugo Hakim Damer Date: Tue, 16 Sep 2025 23:48:19 +0200 Subject: [PATCH 02/13] chore: update JetBrains run configs, add VS Code launch.json --- .idea/editor.xml | 248 ++++++++++++++++++ .idea/misc.xml | 6 + .../Test_libcoap_rs__GnuTLS_.xml | 22 ++ .../Test_libcoap_rs__MbedTLS_.xml | 22 ++ .../Test_libcoap_rs__OpenSSL_.xml | 22 ++ ...Test.xml => Test_libcoap_rs__no_DTLS_.xml} | 5 +- .../Test_libcoap_rs__tinyDTLS_.xml | 22 ++ .../Test_libcoap_rs__wolfSSL_.xml | 22 ++ .../Test_libcoap_sys__GnuTLS_.xml | 22 ++ .../Test_libcoap_sys__MbedTLS_.xml | 22 ++ .../Test_libcoap_sys__OpenSSL_.xml | 22 ++ .../Test_libcoap_sys__tinyDTLS_.xml | 22 ++ .../Test_libcoap_sys__wolfSSL_.xml | 22 ++ .vscode/launch.json | 156 +++++++++++ 14 files changed, 633 insertions(+), 2 deletions(-) create mode 100644 .idea/editor.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/runConfigurations/Test_libcoap_rs__GnuTLS_.xml create mode 100644 .idea/runConfigurations/Test_libcoap_rs__MbedTLS_.xml create mode 100644 .idea/runConfigurations/Test_libcoap_rs__OpenSSL_.xml rename .idea/runConfigurations/{Test.xml => Test_libcoap_rs__no_DTLS_.xml} (76%) create mode 100644 .idea/runConfigurations/Test_libcoap_rs__tinyDTLS_.xml create mode 100644 .idea/runConfigurations/Test_libcoap_rs__wolfSSL_.xml create mode 100644 .idea/runConfigurations/Test_libcoap_sys__GnuTLS_.xml create mode 100644 .idea/runConfigurations/Test_libcoap_sys__MbedTLS_.xml create mode 100644 .idea/runConfigurations/Test_libcoap_sys__OpenSSL_.xml create mode 100644 .idea/runConfigurations/Test_libcoap_sys__tinyDTLS_.xml create mode 100644 .idea/runConfigurations/Test_libcoap_sys__wolfSSL_.xml create mode 100644 .vscode/launch.json diff --git a/.idea/editor.xml b/.idea/editor.xml new file mode 100644 index 00000000..ead1d8a3 --- /dev/null +++ b/.idea/editor.xml @@ -0,0 +1,248 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..369389b4 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Test_libcoap_rs__GnuTLS_.xml b/.idea/runConfigurations/Test_libcoap_rs__GnuTLS_.xml new file mode 100644 index 00000000..0493d347 --- /dev/null +++ b/.idea/runConfigurations/Test_libcoap_rs__GnuTLS_.xml @@ -0,0 +1,22 @@ + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Test_libcoap_rs__MbedTLS_.xml b/.idea/runConfigurations/Test_libcoap_rs__MbedTLS_.xml new file mode 100644 index 00000000..0696b480 --- /dev/null +++ b/.idea/runConfigurations/Test_libcoap_rs__MbedTLS_.xml @@ -0,0 +1,22 @@ + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Test_libcoap_rs__OpenSSL_.xml b/.idea/runConfigurations/Test_libcoap_rs__OpenSSL_.xml new file mode 100644 index 00000000..309f693e --- /dev/null +++ b/.idea/runConfigurations/Test_libcoap_rs__OpenSSL_.xml @@ -0,0 +1,22 @@ + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Test.xml b/.idea/runConfigurations/Test_libcoap_rs__no_DTLS_.xml similarity index 76% rename from .idea/runConfigurations/Test.xml rename to .idea/runConfigurations/Test_libcoap_rs__no_DTLS_.xml index 4f6e6f9d..9dd7e398 100644 --- a/.idea/runConfigurations/Test.xml +++ b/.idea/runConfigurations/Test_libcoap_rs__no_DTLS_.xml @@ -1,6 +1,7 @@ - -