Skip to content

Commit

Permalink
alphabetically sort packages for nixpkgs
Browse files Browse the repository at this point in the history
  • Loading branch information
TyberiusPrime committed Nov 18, 2024
1 parent e059473 commit fa0b651
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
6 changes: 3 additions & 3 deletions examples/basic/anysnake2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

[nixpkgs]
# the nixpkgs used inside the container
packages = [ # use https://search.nixos.org/packages to search
"fish",
]
packages = [
"bash",
"fish"]
url = "github:NixOS/nixpkgs/master/24.05"


Expand Down
9 changes: 6 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub struct ConfigToml {
pub struct TofuConfigToml {
pub anysnake2_toml_path: Option<PathBuf>,
pub anysnake2: TofuAnysnake2,
pub nixpkgs: TofuNixpkgs,
pub nixpkgs: TofuNixPkgs,
pub outside_nixpkgs: TofuVCS,
pub ancient_poetry: TofuVCS,
pub poetry2nix: TofuPoetry2Nix,
Expand Down Expand Up @@ -291,10 +291,13 @@ impl NixPkgs {
pub fn default_allow_unfree() -> bool {
false
}



}

#[derive(Debug)]
pub struct TofuNixpkgs {
#[derive(Debug, Clone)]
pub struct TofuNixPkgs {
pub url: TofuVCS,
pub packages: Vec<String>,
pub allow_unfree: bool,
Expand Down
2 changes: 1 addition & 1 deletion src/flake_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ fn nix_format(input: &str, flake_dir: impl AsRef<Path>) -> Result<String> {
#[allow(clippy::too_many_arguments)]
fn ancient_poetry(
ancient_poetry: &vcs::TofuVCS,
nixpkgs: &config::TofuNixpkgs,
nixpkgs: &config::TofuNixPkgs,
python_packages: &HashMap<String, config::TofuPythonPackageDefinition>,
python_package_definitions: &toml::Table,
pyproject_toml_path: &Path,
Expand Down
46 changes: 34 additions & 12 deletions src/tofu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use toml_edit::value;
use log::{debug, error, info, warn};

use crate::{
config::{self, TofuAnysnake2, TofuConfigToml, TofuDevShell, TofuVCSorDev},
config::{self, TofuAnysnake2, TofuConfigToml, TofuDevShell, TofuVCSorDev},
vcs::{self, BranchOrTag, ParsedVCS, TofuVCS},
};
use anysnake2::util::{change_toml_file, get_proxy_req, TomlUpdates};
Expand Down Expand Up @@ -91,13 +91,16 @@ impl Tofu<config::TofuConfigToml> for config::ConfigToml {
}
},

nixpkgs: self.nixpkgs.tofu_to_tag(
&["nixpkgs", "url"],
updates,
"github:NixOS/nixpkgs", // prefer github:/ for then nix doesn't clone the whole
// repo..
NIXPKGS_TAG_REGEX,
)?,
nixpkgs: self
.nixpkgs
.tofu_to_tag(
&["nixpkgs", "url"],
updates,
"github:NixOS/nixpkgs", // prefer github:/ for then nix doesn't clone the whole
// repo..
NIXPKGS_TAG_REGEX,
)?
.sort_packages(&["nixpkgs", "packages"], updates),
outside_nixpkgs: self.outside_nixpkgs.tofu_to_tag(
&["outside_nixpkgs", "url"],
updates,
Expand Down Expand Up @@ -297,14 +300,14 @@ trait TofuToTag<A> {
) -> Result<A>;
}

impl TofuToTag<config::TofuNixpkgs> for Option<config::NixPkgs> {
impl TofuToTag<config::TofuNixPkgs> for Option<config::NixPkgs> {
fn tofu_to_tag(
self,
toml_name: &[&str],
updates: &mut TomlUpdates,
default_url: &str,
tag_regex: &str,
) -> Result<config::TofuNixpkgs> {
) -> Result<config::TofuNixPkgs> {
let inner_self = self.unwrap_or_else(config::NixPkgs::new);

let url_and_rev: vcs::ParsedVCS = inner_self
Expand All @@ -318,7 +321,7 @@ impl TofuToTag<config::TofuNixpkgs> for Option<config::NixPkgs> {
tag_regex,
)?;

let out = config::TofuNixpkgs {
let out = config::TofuNixPkgs {
url: url_and_rev,
packages: inner_self.packages.unwrap_or_default(),
allow_unfree: inner_self.allow_unfree,
Expand Down Expand Up @@ -606,7 +609,7 @@ fn _tofu_repo_to_tag(
tag_regex: &str,
) -> Result<vcs::TofuVCS> {
let input = input.unwrap_or_else(|| default_url.try_into().expect("invalid default url"));
debug!("tofu_repo_to_tag: {toml_name:?} from {input:?} with /{tag_regex}/");
//debug!("tofu_repo_to_tag: {toml_name:?} from {input:?} with /{tag_regex}/");
let (changed, out) = match &input {
vcs::ParsedVCS::Git {
url,
Expand Down Expand Up @@ -1386,3 +1389,22 @@ impl Tofu<TofuDevShell> for Option<config::DevShell> {
Ok(res)
}
}

trait SortPackages {
fn sort_packages(self, toml_name: &[&str], updates: &mut TomlUpdates) -> Self;
}

impl SortPackages for config::TofuNixPkgs {
fn sort_packages(self, toml_name: &[&str], updates: &mut TomlUpdates) -> Self {
let mut out = self.clone();
out.packages.sort();
if out.packages != self.packages {
let op: toml_edit::Array = out.packages.iter().map(ToString::to_string).collect();
updates.push((
toml_name.iter().map(ToString::to_string).collect(),
value(op),
));
}
out
}
}

0 comments on commit fa0b651

Please sign in to comment.