Skip to content

Commit

Permalink
docs: Added rust example
Browse files Browse the repository at this point in the history
  • Loading branch information
htngr committed Dec 19, 2024
1 parent 847f6a8 commit cfb62fd
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 8 deletions.
Empty file.
Empty file.
Empty file.
4 changes: 2 additions & 2 deletions docs/content/3.config/6.environments/gpu.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# GPU

::: tip
::alert{type="info"}
Try it out with `codchi init <NAME> https://github.com/aformatik/codchi nixosModules.gpu`.
:::
::

GPU usage should work by default inside a code machine. Codchi uses the GPU driver from your host and therefore only works when the driver is installed on your host. Currently only Nvidia/CUDA is tested.

Expand Down
4 changes: 2 additions & 2 deletions docs/content/3.config/6.environments/javascript.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Javascript

::: tip
::alert{type="info"}
Try it out with `codchi init <NAME> https://github.com/aformatik/codchi nixosModules.javascript`.
:::
::

```nix
{ pkgs, ... }: {
Expand Down
4 changes: 2 additions & 2 deletions docs/content/3.config/6.environments/jvm.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# JVM

::: tip
::alert{type="info"}
Try it out with `codchi init <NAME> https://github.com/aformatik/codchi nixosModules.jvm`.
:::
::


## General, Java & Kotlin
Expand Down
4 changes: 2 additions & 2 deletions docs/content/3.config/6.environments/python.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Python

::: tip
::alert{type="info"}
Try it out with `codchi init <NAME> https://github.com/aformatik/codchi nixosModules.python`.
:::
::

```nix
{ pkgs, ... }: {
Expand Down
116 changes: 116 additions & 0 deletions docs/content/3.config/6.environments/rust.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Rust

## Rust via flakes

::alert{type="info"}
Try it out with `codchi clone <NAME> https://github.com/aformatik/codchi nixosModules.codchi`.
::

There is already great support for Rust development environments via Nix Flakes. Additionally to providing a development environment, this method also allows building the Rust project itself. Codchi itself uses this approach.

To use this method with Codchi, just install an IDE (and optionally nix-direnv) like [Codchi's code machine](https://github.com/aformatik/codchi/blob/master/configuration.nix) does:

```nix
{ pkgs, ... }:
{
environment.systemPackages = [
(pkgs.vscode-with-extensions.override {
vscode = pkgs.vscodium;
vscodeExtensions = with pkgs.vscode-extensions; [
rust-lang.rust-analyzer
jnoortheen.nix-ide
mkhl.direnv
asvetliakov.vscode-neovim
];
})
pkgs.bashInteractive # fix terminal in VSCode
];
programs.neovim.enable = true; # needed for vscode-neovim plugin
programs.direnv = {
enable = true;
nix-direnv.enable = true;
};
}
```

When not using direnv and its IDE integration, the IDE must be started from within the Nix Flakes devshell:
```bash
cd my/project
nix develop
codium .
```

## Rust via NixOS

Cargo can also be installed globally. For this we recommend [oxalica/rust-overlay](https://github.com/oxalica/rust-overlay), a rustup-like distribution of rust within the Nix ecosystem.

::alert{type="info"}
Try it out with `codchi init <NAME> https://github.com/aformatik/codchi nixosModules.rust`.
::


```nix
{ pkgs, ... }: {
# https://github.com/oxalica/rust-overlay
nixpkgs.overlays = [
(self: super:
let
overlay = super.fetchFromGitHub {
repo = "rust-overlay";
owner = "oxalica";
sha256 = "1bp1k5qla5gwh6vc50m5pcwdfxn6g703yx1i8qrjs4l7kgh3y507";
rev = "573c674a3ad06e8a525263185ebef336a411d1d5";
};
in
{
inherit (import overlay self super) rust-bin;
}
)
];
environment.systemPackages = [
pkgs.gcc
# https://github.com/oxalica/rust-overlay
(
let
rustConfig = {
extensions = [
"rust-src"
"rust-analyzer"
];
targets = [
"x86_64-unknown-linux-gnu"
"wasm32-wasi"
];
};
in
# stable
pkgs.rust-bin.stable.latest.default.override rustConfig
# nightly
# pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override rustConfig);
)
# You can also install IJ with plugins (experimental).
# For more information see <https://github.com/NixOS/nixpkgs/tree/nixos-24.05/pkgs/applications/editors/jetbrains>
(with pkgs.jetbrains; plugins.addPlugins rust-rover [
"nixidea"
])
# When using VSCode instead of RustRover
# (pkgs.vscode-with-extensions.override {
# vscode = pkgs.vscodium;
# vscodeExtensions = with pkgs.vscode-extensions; [
# rust-lang.rust-analyzer
# jnoortheen.nix-ide
# ];
# })
# pkgs.bashInteractive # fix terminal in VSCode
];
# For proprietary apps like RustRover
nixpkgs.config.allowUnfree = true;
}
```
62 changes: 62 additions & 0 deletions nix/examples/rust/configuration.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{ pkgs, ... }: {

# https://github.com/oxalica/rust-overlay
nixpkgs.overlays = [
(self: super:
let
overlay = super.fetchFromGitHub {
repo = "rust-overlay";
owner = "oxalica";
sha256 = "1bp1k5qla5gwh6vc50m5pcwdfxn6g703yx1i8qrjs4l7kgh3y507";
rev = "573c674a3ad06e8a525263185ebef336a411d1d5";
};
in
{
inherit (import overlay self super) rust-bin;
}
)
];

environment.systemPackages = [

pkgs.gcc

# https://github.com/oxalica/rust-overlay
(
let
rustConfig = {
extensions = [
"rust-src"
"rust-analyzer"
];
targets = [
"x86_64-unknown-linux-gnu"
"wasm32-wasi"
];
};
in
# stable
pkgs.rust-bin.stable.latest.default.override rustConfig
# nightly
# pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override rustConfig);
)

# You can also install IJ with plugins (experimental).
# For more information see <https://github.com/NixOS/nixpkgs/tree/nixos-24.05/pkgs/applications/editors/jetbrains>
(with pkgs.jetbrains; plugins.addPlugins rust-rover [
"nixidea"
])

(pkgs.vscode-with-extensions.override {
vscode = pkgs.vscodium;
vscodeExtensions = with pkgs.vscode-extensions; [
rust-lang.rust-analyzer
jnoortheen.nix-ide
];
})
pkgs.bashInteractive # fix terminal in VSCode
];

# For proprietary apps like RustRover
nixpkgs.config.allowUnfree = true;
}
6 changes: 6 additions & 0 deletions nix/examples/rust/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs = { ... }: {
nixosModules.default = import ./configuration.nix;
};
}

0 comments on commit cfb62fd

Please sign in to comment.