From 6b5124442c4e0fb91b33d2242d32c8f9b7f3faaf Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Fri, 7 Apr 2023 23:01:58 +0100 Subject: [PATCH] Initial commit --- .github/workflows/rust.yml | 196 +++++++++++++++++++++++++++++++++++++ .gitignore | 5 + Cargo.lock | 7 ++ Cargo.toml | 8 ++ flake.lock | 128 ++++++++++++++++++++++++ flake.nix | 111 +++++++++++++++++++++ src/main.rs | 3 + 7 files changed, 458 insertions(+) create mode 100644 .github/workflows/rust.yml create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 src/main.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..f50de2f --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,196 @@ +name: Rust + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +env: + CARGO_TERM_COLOR: always + +jobs: + build: + { + "runs-on": "ubuntu-latest", + "steps": [ + { + "uses": "actions/checkout@v3", + "name": "Checkout" + }, + { + "name": "Install Nix", + "uses": "cachix/install-nix-action@v17", + "with": { "extra-nix-config": "access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}" } + }, + { + "name": "cargo test", + "run": "nix develop --command cargo test --verbose" + } + ] + } + + build-release: + { + "runs-on": "ubuntu-latest", + "steps": [ + { + "uses": "actions/checkout@v3", + "name": "Checkout" + }, + { + "name": "Install Nix", + "uses": "cachix/install-nix-action@v17", + "with": { "extra-nix-config": "access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}" } + }, + { + "name": "cargo test (release)", + "run": "nix develop --command cargo test --verbose --release" + } + ] + } + + alejandra: + { + "name": "Nix formatting", + "runs-on": "ubuntu-latest", + "steps": [ + { + "uses": "actions/checkout@v3", + "name": "Checkout" + }, + { + "name": "Install Nix", + "uses": "cachix/install-nix-action@v17", + "with": { "extra-nix-config": "access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}" } + }, + { + "name": "Run Alejandra", + "run": "nix develop .#ci --command alejandra -- --check ." + } + ] + } + + rust-fmt: + { + "name": "Rust formatting", + "runs-on": "ubuntu-latest", + "steps": [ + { + "uses": "actions/checkout@v3", + "name": "Checkout" + }, + { + "name": "Install Nix", + "uses": "cachix/install-nix-action@v17", + "with": { "extra-nix-config": "access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}" } + }, + { + "name": "Run formatter", + "run": "nix develop --command cargo -- fmt --all -- --check" + } + ] + } + + clippy: + { + "name": "Clippy", + "runs-on": "ubuntu-latest", + "steps": [ + { + "uses": "actions/checkout@v3", + "name": "Checkout" + }, + { + "name": "Install Nix", + "uses": "cachix/install-nix-action@v17", + "with": { "extra-nix-config": "access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}" } + }, + { + "name": "Run Clippy", + "run": "nix develop --command cargo -- clippy -- -D warnings" + } + ] + } + + nix-build: + { + "name": "Nix build", + "runs-on": "ubuntu-latest", + "steps": [ + { + "uses": "actions/checkout@v3", + "name": "Checkout" + }, + { + "name": "Install Nix", + "uses": "cachix/install-nix-action@v17", + "with": { "extra-nix-config": "access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}" } + }, + { + "name": "Build app", + "run": "nix build" + } + ] + } + + nix-run: + { + "name": "Run app through Nix", + "runs-on": "ubuntu-latest", + "steps": [ + { + "uses": "actions/checkout@v3", + "name": "Checkout" + }, + { + "name": "Install Nix", + "uses": "cachix/install-nix-action@v17", + "with": { "extra-nix-config": "access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}" } + }, + { + "name": "Run app", + "run": "nix run" + } + ] + } + + linkcheck: + { + "name": "Check links", + "runs-on": "ubuntu-latest", + "steps": [ + { + "uses": "actions/checkout@v3", + "name": "Checkout" + }, + { + "name": "Install Nix", + "uses": "cachix/install-nix-action@v17", + "with": { "extra-nix-config": "access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}" } + }, + { + "name": "Run link checker", + "run": "nix develop .#ci --command markdown-link-check README.md" + } + ] + } + + all-required-checks-complete: + { + "needs": + [ + 'nix-build', + 'nix-run', + 'build', + 'build-release', + 'alejandra', + 'clippy', + 'rust-fmt', + 'linkcheck', + ], + "runs-on": "ubuntu-latest", + "steps": [ + { "run": "echo \"All required checks complete.\"" } + ] + } diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ca319f7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +target/ +.idea/ +*.iml +.vscode/ +.profile* diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..5f697fb --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "performance_aware_programming" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2fde269 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "performance_aware_programming" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..f3f7ab2 --- /dev/null +++ b/flake.lock @@ -0,0 +1,128 @@ +{ + "nodes": { + "crate2nix": { + "flake": false, + "locked": { + "lastModified": 1678820090, + "narHash": "sha256-4FFeonajXNqiSP0PqUlC1wmTyPC8SX6SthEvgRjMupw=", + "owner": "kolloch", + "repo": "crate2nix", + "rev": "4fd826e37bec90dfe26ca16cb6e632c617cc7c0d", + "type": "github" + }, + "original": { + "owner": "kolloch", + "repo": "crate2nix", + "type": "github" + } + }, + "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" + } + }, + "flake-utils": { + "locked": { + "lastModified": 1659877975, + "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1679705136, + "narHash": "sha256-MDlZUR7wJ3PlPtqwwoGQr3euNOe0vdSSteVVOef7tBY=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "8f40f2f90b9c9032d1b824442cfbbe0dbabd0dbd", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1679734080, + "narHash": "sha256-z846xfGLlon6t9lqUzlNtBOmsgQLQIZvR6Lt2dImk1M=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "dbf5322e93bcc6cfc52268367a8ad21c09d76fea", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "crate2nix": "crate2nix", + "flake-compat": "flake-compat", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay", + "utils": "utils" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1679106165, + "narHash": "sha256-03Opt2yu4E/AIFjvlgib0/nhMn6B4B/t/nvwS2bzOGw=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "7313c06ac334d6262ddfe30a38b3abc3da6bd565", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "utils": { + "locked": { + "lastModified": 1678901627, + "narHash": "sha256-U02riOqrKKzwjsxc/400XnElV+UtPUQWpANPlyazjH0=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "93a2b84fc4b70d9e089d029deacc3583435c2ed6", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..a308f16 --- /dev/null +++ b/flake.nix @@ -0,0 +1,111 @@ +{ + description = "Coursework for Performance-Aware Programming"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + utils.url = "github:numtide/flake-utils"; + rust-overlay.url = "github:oxalica/rust-overlay"; + crate2nix = { + url = "github:kolloch/crate2nix"; + flake = false; + }; + flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; + }; + + outputs = { + self, + nixpkgs, + utils, + rust-overlay, + crate2nix, + ... + }: let + name = "performance_aware_programming"; + in + utils.lib.eachDefaultSystem + ( + system: let + # Imports + pkgs = import nixpkgs { + inherit system; + overlays = [ + rust-overlay.overlays.default + (self: super: { + # Because rust-overlay bundles multiple rust packages into one + # derivation, specify that mega-bundle here, so that crate2nix + # will use them automatically. + rustc = self.rust-bin.nightly.latest.default; + cargo = self.rust-bin.nightly.latest.default; + }) + ]; + }; + inherit + (import "${crate2nix}/tools.nix" {inherit pkgs;}) + generatedCargoNix + ; + + # Create the cargo2nix project + project = + pkgs.callPackage + (generatedCargoNix { + inherit name; + src = ./.; + }) + { + # Individual crate overrides go here + # Example: https://github.com/balsoft/simple-osd-daemons/blob/6f85144934c0c1382c7a4d3a2bbb80106776e270/flake.nix#L28-L50 + defaultCrateOverrides = + pkgs.defaultCrateOverrides + // { + # The app crate itself is overriden here. Typically we + # configure non-Rust dependencies (see below) here. + ${name} = oldAttrs: + { + inherit buildInputs nativeBuildInputs; + } + // buildEnvVars; + }; + }; + + # Configuration for the non-Rust dependencies + buildInputs = with pkgs; [openssl.dev]; + nativeBuildInputs = with pkgs; [rustc cargo pkgconfig]; + buildEnvVars = { + PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig"; + }; + in rec { + packages.${name} = project.workspaceMembers.${name}.build; + + # `nix build` + defaultPackage = packages.${name}; + + # `nix run` + apps.${name} = utils.lib.mkApp { + inherit name; + drv = packages.${name}; + }; + defaultApp = apps.${name}; + + # `nix develop` + devShells = { + ci = + pkgs.mkShell { + inherit nativeBuildInputs; + buildInputs = [pkgs.nodePackages.markdown-link-check pkgs.alejandra] ++ buildInputs; + RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; + } + // buildEnvVars; + default = + pkgs.mkShell + { + inherit buildInputs nativeBuildInputs; + RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; + } + // buildEnvVars; + }; + } + ); +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}