Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shell.nix: Add nix shell #519

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Licensed under the Apache License, Version 2.0 or the MIT License.
# SPDX-License-Identifier: Apache-2.0 OR MIT
# Copyright Tock Contributors 2022.

# Shell expression for the Nix package manager
#
# This nix expression creates an environment with necessary packages installed:
#
# * `tockloader`
# * rust
#
# To use:
#
# $ nix-shell
#

{ pkgs ? import <nixpkgs> { }, withUnfreePkgs ? false }:

with builtins;
let
inherit (pkgs) stdenv lib;

# Tockloader v1.11.0
tockloader = import (pkgs.fetchFromGitHub {
owner = "tock";
repo = "tockloader";
rev = "v1.11.0";
sha256 = "sha256-bPEfpfOZOjOiazqRgn1cnqe4ohLPvocuENKoZx/Qw80=";
}) { inherit pkgs withUnfreePkgs; };

# Rust toolchain overlay
rust_overlay = import "${pkgs.fetchFromGitHub {
owner = "nix-community";
repo = "fenix";
# Revision hash must correspond with the same date as the nightly version.
rev = "ffa0a8815be591767f82d42c63d88bfa4026a967";
sha256 = "sha256-JOrXleSdEKuymCyxg7P4GTTATDhBdfeyWcd1qQQlIYw=";
}}/overlay.nix";

nixpkgs = import <nixpkgs> { overlays = [ rust_overlay ]; };
stable = (lib.importTOML ./rust-toolchain.toml).toolchain;
nightly = (lib.importTOML ./nightly/rust-toolchain.toml).toolchain;

# Use nightly development toolchain because Miri is not supported
# by the MSRV (Minimum Supported Rust Version) toolchain.
nightlyToolchain = nixpkgs.fenix.fromToolchainName {
name = nightly.channel;
sha256 = "sha256-R/ONZzJaWQr0pl5RoXFIbnxIE3m6oJWy/rr2W0wXQHQ=";
};
Comment on lines +44 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate your efforts in making this work by combining the two toolchains, and while I think that this is a decent approach generally, to me this really points to a deficiency in how we pick nightly toolchain versions for libtock-rs. For the Tock kernel, we have a script that searches for a Nightly that has all of the required components (including Miri). I think we should rather choose a proper nightly this way, and avoid mix- and matching Rust components. I'm not even sure whether Miri is portable across rustc versions (e.g., in the features that it recognizes).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the dual toolchain installation from before. Only the nightly derivation is generated, with the references on lines 41-42 being to the pure text of the file. This Nix logic has the (virtually) same effect as applying the diff in my original post before doing the toolchain derivation generation.
The problem you foresaw with some components being unavailable was not an issue. make test works, but I agree that incompatible nightlies are a risk in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I'm having trouble matching what you write with the current Nix expression of this PR. Lines lines 51-57 still provide you a toolchain that is efffectively mix-and-match between stable and binary, and what you get is dependent on which components these two (stable + nightly) toolchains happen to provide, right?

And lines 58-65 then also throw in some components (namely, rust-std) taken from whatever the fenix overlay defines as the latest toolchain, which only happens to coincide with the pinned nightly toolchain by the correctness of the condition in lines 35? That seems very brittle.


# Combine the components from the stable and nightly toolchains.
combinedToolchain =
nightlyToolchain.withComponents (
nightly.components ++
stable.components ++
[ "cargo" ]
);
# Add the cross-compile target platforms to the environment.
rustBuild =
nixpkgs.fenix.combine (
foldl'
(acc: item: acc ++ [ nixpkgs.fenix.targets.${item}.latest.rust-std ])
[ combinedToolchain ]
stable.targets
);

in
pkgs.mkShell {
name = "tock-dev";

buildInputs = with pkgs; [
# --- Toolchains ---
rustBuild
openocd

# --- Convenience and support packages ---
python3Full
tockloader

# Required for tools/print_tock_memory_usage.py
python3Packages.cxxfilt


# --- CI support packages ---
qemu

# --- Flashing tools ---
# If your board requires J-Link to flash and you are on NixOS,
# add these lines to your system wide configuration.

# Enable udev rules from segger-jlink package
# services.udev.packages = [
# pkgs.segger-jlink
# ];

# Add "segger-jlink" to your system packages and accept the EULA:
# nixpkgs.config.segger-jlink.acceptLicense = true;
];

LD_LIBRARY_PATH = "${stdenv.cc.cc.lib}/lib64:$LD_LIBRARY_PATH";

# Instruct the Tock gnumake-based build system to not check for rustup and
# assume all requirend tools are installed and available in the $PATH
NO_RUSTUP = "1";

# The defaults "objcopy" and "objdump" are wrong (stem from the standard
# environment for x86), use "llvm-obj{copy,dump}" as defined in the makefile
shellHook = ''
unset OBJCOPY
unset OBJDUMP
'';
}