From cfc01650b395fe0cfda5355b2cd5b1395251df5c Mon Sep 17 00:00:00 2001 From: mehbark Date: Wed, 22 Apr 2026 22:58:25 -0400 Subject: [PATCH] it's a flake now --- .direnv/bin/nix-direnv-reload | 19 ++++++++ Cargo.toml | 2 + default.nix | 55 ++++++++++++++++++++++ flake.lock | 88 +++++++++++++++++++++++++++++++++++ flake.nix | 79 +++++++++++++++++++++++++++++++ src/main.rs | 2 +- 6 files changed, 244 insertions(+), 1 deletion(-) create mode 100755 .direnv/bin/nix-direnv-reload create mode 100644 default.nix create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.direnv/bin/nix-direnv-reload b/.direnv/bin/nix-direnv-reload new file mode 100755 index 0000000..6361d7a --- /dev/null +++ b/.direnv/bin/nix-direnv-reload @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -e +if [[ ! -d "/Users/clark/Aims/rust/diffie-hellman" ]]; then + echo "Cannot find source directory; Did you move it?" + echo "(Looking for "/Users/clark/Aims/rust/diffie-hellman")" + echo 'Cannot force reload with this script - use "direnv reload" manually and then try again' + exit 1 +fi + +# rebuild the cache forcefully +_nix_direnv_force_reload=1 direnv exec "/Users/clark/Aims/rust/diffie-hellman" true + +# Update the mtime for .envrc. +# This will cause direnv to reload again - but without re-building. +touch "/Users/clark/Aims/rust/diffie-hellman/.envrc" + +# Also update the timestamp of whatever profile_rc we have. +# This makes sure that we know we are up to date. +touch -r "/Users/clark/Aims/rust/diffie-hellman/.envrc" "/Users/clark/Aims/rust/diffie-hellman/.direnv"/*.rc diff --git a/Cargo.toml b/Cargo.toml index 5707673..3951efc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,8 @@ name = "diffie-hellman" version = "0.1.0" edition = "2024" +description = "toy implementation of diffie-hellman key exchange" +homepage = "https://g.pyrope.net/diffie-hellman" [dependencies] ntt = "0.1.9" diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..83f9f14 --- /dev/null +++ b/default.nix @@ -0,0 +1,55 @@ +{ lib +, naersk +, stdenv +, clangStdenv +, hostPlatform +, targetPlatform +, pkg-config +, libiconv +, rustfmt +, cargo +, rustc + # , llvmPackages # Optional + # , protobuf # Optional +}: + +let + cargoToml = (builtins.fromTOML (builtins.readFile ./Cargo.toml)); +in + +naersk.lib."${targetPlatform.system}".buildPackage rec { + src = ./.; + + buildInputs = [ + rustfmt + pkg-config + cargo + rustc + libiconv + ]; + checkInputs = [ cargo rustc ]; + + doCheck = true; + CARGO_BUILD_INCREMENTAL = "false"; + RUST_BACKTRACE = "full"; + copyLibs = true; + + # Optional things you might need: + # + # If you depend on `libclang`: + # LIBCLANG_PATH = "${llvmPackages.libclang}/lib"; + # + # If you depend on protobuf: + # PROTOC = "${protobuf}/bin/protoc"; + # PROTOC_INCLUDE = "${protobuf}/include"; + + name = cargoToml.package.name; + version = cargoToml.package.version; + + meta = with lib; { + description = cargoToml.package.description; + homepage = cargoToml.package.homepage; + license = with licenses; [ mit ]; + maintainers = with maintainers; [ ]; + }; +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..97c81eb --- /dev/null +++ b/flake.lock @@ -0,0 +1,88 @@ +{ + "nodes": { + "fenix": { + "inputs": { + "nixpkgs": [ + "naersk", + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1752475459, + "narHash": "sha256-z6QEu4ZFuHiqdOPbYss4/Q8B0BFhacR8ts6jO/F/aOU=", + "owner": "nix-community", + "repo": "fenix", + "rev": "bf0d6f70f4c9a9cf8845f992105652173f4b617f", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, + "naersk": { + "inputs": { + "fenix": "fenix", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1776200608, + "narHash": "sha256-broZ6RFQr4Fv0wT73gGmzNX14A43TmTFF8g4wDKlNss=", + "owner": "nmattia", + "repo": "naersk", + "rev": "8b23250ab45c2a38cd91031aee26478ca4d0a28e", + "type": "github" + }, + "original": { + "owner": "nmattia", + "repo": "naersk", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1776329215, + "narHash": "sha256-a8BYi3mzoJ/AcJP8UldOx8emoPRLeWqALZWu4ZvjPXw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "b86751bc4085f48661017fa226dee99fab6c651b", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "naersk": "naersk", + "nixpkgs": "nixpkgs" + } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1752428706, + "narHash": "sha256-EJcdxw3aXfP8Ex1Nm3s0awyH9egQvB2Gu+QEnJn2Sfg=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "591e3b7624be97e4443ea7b5542c191311aa141d", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..33a7ca4 --- /dev/null +++ b/flake.nix @@ -0,0 +1,79 @@ +# https://hoverbear.org/blog/a-flake-for-your-crate/ +{ + description = "Diffie hellman"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + naersk.url = "github:nmattia/naersk"; + naersk.inputs.nixpkgs.follows = "nixpkgs"; + }; + + outputs = { self, nixpkgs, naersk }: + let + cargoToml = (builtins.fromTOML (builtins.readFile ./Cargo.toml)); + supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; + forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system: f system); + in + { + overlay = final: prev: { + "${cargoToml.package.name}" = final.callPackage ./. { inherit naersk; }; + }; + + packages = forAllSystems (system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ + self.overlay + ]; + }; + in + { + "${cargoToml.package.name}" = pkgs."${cargoToml.package.name}"; + }); + + + defaultPackage = forAllSystems (system: (import nixpkgs { + inherit system; + overlays = [ self.overlay ]; + })."${cargoToml.package.name}"); + + checks = forAllSystems (system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ + self.overlay + ]; + }; + in + { + format = pkgs.runCommand "check-format" + { + buildInputs = with pkgs; [ rustfmt cargo ]; + } '' + ${pkgs.rustfmt}/bin/cargo-fmt fmt --manifest-path ${./.}/Cargo.toml -- --check + ${pkgs.nixpkgs-fmt}/bin/nixpkgs-fmt --check ${./.} + touch $out # it worked! + ''; + "${cargoToml.package.name}" = pkgs."${cargoToml.package.name}"; + }); + devShells = forAllSystems (system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ self.overlay ]; + }; + in + pkgs.mkShell { + inputsFrom = with pkgs; [ + pkgs."${cargoToml.package.name}" + ]; + buildInputs = with pkgs; [ + rustfmt + nixpkgs-fmt + ]; + LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib"; + }); + }; +} diff --git a/src/main.rs b/src/main.rs index 094f8d5..d4e7ebc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ fn main() { let a = rpassword::prompt_password("a = ").unwrap(); let a = a.parse::().unwrap(); - println!("{}", mod_exp(i64::from(g), i64::from(a), i64::from(p))); + println!("gÂȘ mod p = {}", mod_exp(i64::from(g), i64::from(a), i64::from(p))); } else { let rng = rand::rng();