Skip to content

Instantly share code, notes, and snippets.

@cessationoftime
Last active November 11, 2023 02:31
Show Gist options
  • Save cessationoftime/e4e988059727ba664f8f105bd5526c0c to your computer and use it in GitHub Desktop.
Save cessationoftime/e4e988059727ba664f8f105bd5526c0c to your computer and use it in GitHub Desktop.
# https://github.com/nix-community/nix-direnv
# https://github.com/oxalica/rust-overlay
{
description = "A basic flake with a shell";
nixConfig.bash-prompt = "\[rust-plot\]($(parse_git_branch))$ ";
inputs.nixpkgs-pinned.url = "github:NixOS/nixpkgs?rev=85f1ba3e51676fa8cc604a3d863d729026a6b8eb";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.rust-overlay.url = "github:oxalica/rust-overlay";
outputs = { self, nixpkgs-pinned, rust-overlay, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs-pinned {
inherit system overlays;
};
jetbrains-idea-community-override = pkgs.jetbrains.idea-community.override {
vmopts = "-Xmx8g";
};
#pkgs.rust-bin.beta.latest
#pkgs.rust-bin.stable.latest
#pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default)
#pkgs.rust-bin.beta."2021-01-01"
#pkgs.rust-bin.nightly."2020-12-31"
rust_version = pkgs.rust-bin.stable."1.69.0";
in
with pkgs;
{
devShells.default = mkShell rec {
nativeBuildInputs = [
bashInteractive
cmake gnumake expat pkg-config fontconfig
];
buildInputs = [
rust_version.default
jetbrains-idea-community-override
freetype
libxkbcommon
libGL
# WINIT_UNIX_BACKEND=wayland
wayland
# WINIT_UNIX_BACKEND=x11
xorg.libXcursor
xorg.libXrandr
xorg.libXi
xorg.libX11
## utilities
xorg.xdpyinfo
];
shellHook = ''
parse_git_branch() {
git rev-parse --abbrev-ref HEAD 2> /dev/null
}
export CARGO_HOME="$PWD/.cargo"
export PATH=$PATH:$CARGO_HOME/bin
# XDG environment variable exports are needed for Intellij Idea to work properly when launched from terminal. Only XDG_DATA_HOME is definitely needed.
# export XDG_STATE_HOME="$PWD/.XDG/state"
export XDG_DATA_HOME="$PWD/.XDG/share"
# export XDG_CONFIG_HOME="$PWD/.XDG/config"
# export XDG_CACHE_HOME="$PWD/.XDG/cache"
#using the $PWD for the XDG_DATA_HOME avoids this error:
#/nix/store/hl5v6vjsf3yhi9a2fyf5vnq06w6c257r-rust-default-1.64.0/bin/cargo check --message-format json --workspace --all-targets -Z unstable-options --keep-going
#error: failed to run `rustc` to learn about target-specific information
#error: failed to run `rustc` to learn about target-specific information
#Caused by:
# could not execute process `/home/username/.local/share/JetBrains/IdeaIC2023.1/intellij-rust/bin/linux/x86-64/intellij-rust-native-helper /nix/store/hl5v6vjsf3yhi9a2fyf5vnq06w6c257r-rust-default-1.64.0/bin/rustc - --crate-name ___ --print=file-names --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=cfg` (never executed)
#Caused by:
# No such file or directory (os error 2)
echo ""
echo "impure environment loaded for project: rust-plot"
echo "to enter a pure environment from this one use command: 'nix develop -i'"
echo ""
echo "launch development IDE in this environment with '@idea'"
'';
LD_LIBRARY_PATH = "${nixpkgs-pinned.lib.makeLibraryPath buildInputs}";
RUSTUP_TOOLCHAIN = rust_version.rustc.version;
## an old method to set rust compiler flags. This might be different now that I use the rust overlay.
# RUSTFLAGS = (builtins.map (a: ''-L ${a}/lib'') []);
# RUSTFLAGS =
# "-C link-arg=-Wl,-dynamic-linker,/lib/ld-linux-armhf.so.3 " +
# "-C target-feature=+crt-static";
########################################3
#### variables required when running in pure mode nix develop -i
## Setting this prevents error "Wayland status: XdgRuntimeDirNotSet"
XDG_RUNTIME_DIR="/run/user/1000";
# This corrects another x11 error. this could need to be sometimes changed to DISPLAY=":0";
DISPLAY=":1";
# prevents error: Authorization required, but no authorization protocol specified...Failed to initialize any backend! Wayland status: NoCompositorListening X11 status: XOpenDisplayFailed
XAUTHORITY="/run/user/1000/gdm/Xauthority";
#########################################
# extra debugging flags
#RUST_BACKTRACE=1;
#LIBGL_DEBUG = "verbose";
#WAYLAND_DEBUG=1;
#RUST_LOG="debug";
};
}
);
}
@cessationoftime
Copy link
Author

cessationoftime commented Nov 10, 2023

There are buildinputs and nativebuildinputs in this file that are specifically required for my application. You will want to remove those if you copy this. Best to look at the rust-overlay flake.nix example and add lines to that as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment