Skip to content

Commit d9bf95c

Browse files
authored
Merge pull request #279 from nix-community/ci-refactor
CI: refactor
2 parents 7a739f8 + 68cc383 commit d9bf95c

File tree

16 files changed

+178
-104
lines changed

16 files changed

+178
-104
lines changed

.github/workflows/build.yaml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ on:
88
workflow_dispatch:
99

1010
jobs:
11-
build:
11+
build-linux:
12+
name: "Build NH on Linux"
1213
runs-on: ubuntu-latest
13-
1414
steps:
1515
- uses: cachix/install-nix-action@master
1616
with:
@@ -21,13 +21,8 @@ jobs:
2121
- run: nix build -L --no-link
2222
name: Build
2323

24-
- run: |
25-
eval "$(nix print-dev-env)"
26-
./fix.sh
27-
git diff-index --quiet HEAD
28-
name: Check formatting
29-
30-
Test_Darwin:
24+
build-darwin:
25+
name: "Build NH on Darwin"
3126
runs-on: macos-latest
3227

3328
steps:
@@ -40,6 +35,9 @@ jobs:
4035
- run: nix build -L --no-link
4136
name: Build
4237

38+
# FIXME: this should be moved out of the build workflow. It is **not** a build job
39+
# and opens the door to CI failures due to upstream (nix-darwin) errors. This should
40+
# instead me made into a VM test.
4341
- run: |
4442
mkdir flake
4543
cd flake

.github/workflows/check.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: "Check formating & lints"
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
branches: [ "main" ]
7+
push:
8+
branches-ignore:
9+
- 'update-*'
10+
11+
jobs:
12+
treewide-checks:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: cachix/install-nix-action@master
17+
with:
18+
github_access_token: ${{ secrets.GITHUB_TOKEN }}
19+
20+
- name: Checkout repository
21+
uses: actions/checkout@v4
22+
23+
# Unlike the clippy lints below, this should always pass.
24+
- name: Check Formatting
25+
run: |
26+
# Run cargo fmt in check mode
27+
cargo fmt --check
28+
29+
30+
# We run clippy with lints that help avoid overall low-quality code or what is called "code smell."
31+
# Stylistic lints (e.g., clippy::style and clippy::complexity) are avoided but it is a good idea to
32+
# follow those while working on the codebase.
33+
- name: Clippy Lints
34+
run: |
35+
# Lint Changes
36+
cargo clippy -- -W clippy::pedantic -W clippy::correctness -W clippy::suspicious
37+

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ name = "nh"
33
version = "4.0.3"
44
edition = "2021"
55
license = "EUPL-1.2"
6-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
6+
repository = "https://github.com/nix-community/nh"
7+
description = "Yet Another Nix Helper"
78

89
[dependencies]
910
anstyle = "1.0.0"

fix.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
#! /usr/bin/env bash
22
set -eux
33

4+
echo "Running 'cargo fix' on the codebase"
45
cargo fix --allow-dirty
5-
cargo clippy --fix --allow-dirty
6+
7+
echo "Running clippy linter and applying available fixes"
8+
cargo clippy --fix --allow-dirty \
9+
-W clippy::pedantic \
10+
-W clippy::pedantic \
11+
-W clippy::correctness \
12+
-W clippy::suspicious
13+
14+
echo "Running formatter"
615
cargo fmt
7-
# nix fmt # FIXME

src/clean.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use regex::Regex;
1515
use tracing::{debug, info, instrument, span, warn, Level};
1616
use uzers::os::unix::UserExt;
1717

18-
use crate::{commands::Command, *};
18+
use crate::{commands::Command, interface, Result};
1919

2020
// Nix impl:
2121
// https://github.com/NixOS/nix/blob/master/src/nix-collect-garbage/nix-collect-garbage.cc
@@ -42,12 +42,12 @@ impl interface::CleanMode {
4242
// What profiles to clean depending on the call mode
4343
let uid = nix::unistd::Uid::effective();
4444
let args = match self {
45-
interface::CleanMode::Profile(args) => {
45+
Self::Profile(args) => {
4646
profiles.push(args.profile.clone());
4747
is_profile_clean = true;
4848
&args.common
4949
}
50-
interface::CleanMode::All(args) => {
50+
Self::All(args) => {
5151
if !uid.is_root() {
5252
crate::self_elevate();
5353
}
@@ -72,7 +72,7 @@ impl interface::CleanMode {
7272
}
7373
args
7474
}
75-
interface::CleanMode::User(args) => {
75+
Self::User(args) => {
7676
if uid.is_root() {
7777
bail!("nh clean user: don't run me as root!");
7878
}
@@ -129,7 +129,7 @@ impl interface::CleanMode {
129129
AccessFlags::F_OK | AccessFlags::W_OK,
130130
AtFlags::AT_SYMLINK_NOFOLLOW,
131131
) {
132-
Ok(_) => true,
132+
Ok(()) => true,
133133
Err(errno) => match errno {
134134
Errno::EACCES | Errno::ENOENT => false,
135135
_ => {
@@ -191,7 +191,7 @@ impl interface::CleanMode {
191191
}
192192
println!();
193193
}
194-
for (profile, generations_tagged) in profiles_tagged.iter() {
194+
for (profile, generations_tagged) in &profiles_tagged {
195195
println!("{}", profile.to_string_lossy().blue().bold());
196196
for (gen, tbr) in generations_tagged.iter().rev() {
197197
if *tbr {
@@ -218,7 +218,7 @@ impl interface::CleanMode {
218218
}
219219
}
220220

221-
for (_, generations_tagged) in profiles_tagged.iter() {
221+
for generations_tagged in profiles_tagged.values() {
222222
for (gen, tbr) in generations_tagged.iter().rev() {
223223
if *tbr {
224224
remove_path_nofail(&gen.path);
@@ -324,7 +324,7 @@ fn cleanable_generations(
324324
}
325325

326326
let now = SystemTime::now();
327-
for (gen, tbr) in result.iter_mut() {
327+
for (gen, tbr) in &mut result {
328328
match now.duration_since(gen.last_modified) {
329329
Err(err) => {
330330
warn!(?err, ?now, ?gen, "Failed to compare time!");

src/commands.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ impl Command {
4343
}
4444
}
4545

46-
pub fn elevate(mut self, elevate: bool) -> Self {
46+
pub const fn elevate(mut self, elevate: bool) -> Self {
4747
self.elevate = elevate;
4848
self
4949
}
5050

51-
pub fn dry(mut self, dry: bool) -> Self {
51+
pub const fn dry(mut self, dry: bool) -> Self {
5252
self.dry = dry;
5353
self
5454
}
@@ -163,7 +163,7 @@ pub struct Build {
163163
}
164164

165165
impl Build {
166-
pub fn new(installable: Installable) -> Self {
166+
pub const fn new(installable: Installable) -> Self {
167167
Self {
168168
message: None,
169169
installable,
@@ -183,7 +183,7 @@ impl Build {
183183
self
184184
}
185185

186-
pub fn nom(mut self, yes: bool) -> Self {
186+
pub const fn nom(mut self, yes: bool) -> Self {
187187
self.nom = yes;
188188
self
189189
}

src/completion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use clap_complete::generate;
22
use color_eyre::Result;
33
use tracing::instrument;
44

5+
use crate::interface;
56
use crate::interface::Main;
6-
use crate::*;
77

88
impl interface::CompletionArgs {
99
#[instrument(ret, level = "trace")]

src/darwin.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const CURRENT_PROFILE: &str = "/run/current-system";
1717

1818
impl DarwinArgs {
1919
pub fn run(self) -> Result<()> {
20-
use DarwinRebuildVariant::*;
20+
use DarwinRebuildVariant::{Build, Switch};
2121
match self.subcommand {
2222
DarwinSubcommand::Switch(args) => args.rebuild(Switch),
2323
DarwinSubcommand::Build(args) => {
@@ -38,7 +38,7 @@ enum DarwinRebuildVariant {
3838

3939
impl DarwinRebuildArgs {
4040
fn rebuild(self, variant: DarwinRebuildVariant) -> Result<()> {
41-
use DarwinRebuildVariant::*;
41+
use DarwinRebuildVariant::{Build, Switch};
4242

4343
if nix::unistd::Uid::effective().is_root() {
4444
bail!("Don't run nh os as root. I will call sudo internally as needed");
@@ -121,7 +121,7 @@ impl DarwinRebuildArgs {
121121
}
122122
}
123123

124-
if let Switch = variant {
124+
if matches!(variant, Switch) {
125125
Command::new("nix")
126126
.args(["build", "--no-link", "--profile", SYSTEM_PROFILE])
127127
.arg(out_path.get_path())

src/generations.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct GenerationInfo {
1414
/// Date on switch a generation was built
1515
pub date: String,
1616

17-
/// NixOS version derived from `nixos-version`
17+
/// `NixOS` version derived from `nixos-version`
1818
pub nixos_version: String,
1919

2020
/// Version of the bootable kernel for a given generation
@@ -66,7 +66,7 @@ pub fn describe(generation_dir: &Path, current_profile: &Path) -> Option<Generat
6666
.join("kernel")
6767
.canonicalize()
6868
.ok()
69-
.and_then(|path| path.parent().map(|p| p.to_path_buf()))
69+
.and_then(|path| path.parent().map(std::path::Path::to_path_buf))
7070
.unwrap_or_else(|| PathBuf::from("Unknown"));
7171

7272
let kernel_modules_dir = kernel_dir.join("lib/modules");
@@ -195,9 +195,10 @@ pub fn print_info(mut generations: Vec<GenerationInfo>) {
195195
// Parse all dates at once and cache them
196196
let mut parsed_dates = HashMap::with_capacity(generations.len());
197197
for gen in &generations {
198-
let date = DateTime::parse_from_rfc3339(&gen.date)
199-
.map(|dt| dt.with_timezone(&Local))
200-
.unwrap_or_else(|_| Local.timestamp_opt(0, 0).unwrap());
198+
let date = DateTime::parse_from_rfc3339(&gen.date).map_or_else(
199+
|_| Local.timestamp_opt(0, 0).unwrap(),
200+
|dt| dt.with_timezone(&Local),
201+
);
201202
parsed_dates.insert(
202203
gen.date.clone(),
203204
date.format("%Y-%m-%d %H:%M:%S").to_string(),
@@ -216,7 +217,7 @@ pub fn print_info(mut generations: Vec<GenerationInfo>) {
216217
println!("Error getting current generation!");
217218
}
218219

219-
println!("Closure Size: {}", closure);
220+
println!("Closure Size: {closure}");
220221
println!();
221222

222223
// Determine column widths for pretty printing
@@ -256,7 +257,7 @@ pub fn print_info(mut generations: Vec<GenerationInfo>) {
256257
generation
257258
.specialisations
258259
.iter()
259-
.map(|s| format!("*{}", s))
260+
.map(|s| format!("*{s}"))
260261
.collect::<Vec<String>>()
261262
.join(" ")
262263
};

0 commit comments

Comments
 (0)