diff --git a/compose.go b/compose.go index d230113..9fc600e 100644 --- a/compose.go +++ b/compose.go @@ -123,6 +123,7 @@ type Generator struct { RemoveVolumes bool NoCreateRootTarget bool AutoFormat bool + Formatter string WriteHeader bool NoWriteNixSetup bool DefaultStopTimeout time.Duration @@ -210,6 +211,7 @@ func (g *Generator) Run(ctx context.Context) (*NixContainerConfig, error) { Networks: networks, Volumes: volumes, CreateRootTarget: !g.NoCreateRootTarget, + Formatter: g.Formatter, AutoStart: g.AutoStart, WriteNixSetup: !g.NoWriteNixSetup, AutoFormat: g.AutoFormat, diff --git a/flake.lock b/flake.lock index ae9e070..9f65648 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,50 @@ { "nodes": { + "alejandra": { + "inputs": { + "fenix": "fenix", + "flakeCompat": "flakeCompat", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1730688725, + "narHash": "sha256-g0SSfTWZ5mtMOpQic+eqq9sXMy1E/7yKxxfupZd9V4A=", + "owner": "kamadorueda", + "repo": "alejandra", + "rev": "2bb91e309ca99656addff5c74545acbf5813636d", + "type": "github" + }, + "original": { + "owner": "kamadorueda", + "ref": "3.1.0", + "repo": "alejandra", + "type": "github" + } + }, + "fenix": { + "inputs": { + "nixpkgs": [ + "alejandra", + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1730615655, + "narHash": "sha256-2HBR3zLn57LXKNRtxBb+O+uDqHM4n0pz51rPayMl4cg=", + "owner": "nix-community", + "repo": "fenix", + "rev": "efeb50e2535b17ffd4a135e6e3e5fd60a525180c", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, "flake-utils": { "locked": { "lastModified": 1652776076, @@ -15,6 +60,22 @@ "type": "github" } }, + "flakeCompat": { + "flake": false, + "locked": { + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, "nix-pre-commit": { "inputs": { "flake-utils": "flake-utils", @@ -76,9 +137,27 @@ }, "root": { "inputs": { + "alejandra": "alejandra", "nixpkgs": "nixpkgs", "onchg": "onchg" } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1730555913, + "narHash": "sha256-KNHZUlqsEibg3YtfUyOFQSofP8hp1HKoY+laoesBxRM=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "f17a5bbfd0969ba2e63a74505a80e55ecb174ed9", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 3c82acf..d783c3c 100644 --- a/flake.nix +++ b/flake.nix @@ -5,9 +5,12 @@ nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; onchg.url = "github:aksiksi/onchg-rs"; onchg.inputs.nixpkgs.follows = "nixpkgs"; + + alejandra.url = "github:kamadorueda/alejandra/3.1.0"; + alejandra.inputs.nixpkgs.follows = "nixpkgs"; }; - outputs = { self, nixpkgs, onchg, ... }: let + outputs = { self, nixpkgs, onchg, alejandra, ... }: let supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ]; forAllSystems = nixpkgs.lib.genAttrs supportedSystems; pkgsFor = system: nixpkgs.legacyPackages.${system}; @@ -17,6 +20,8 @@ version = "0.3.2-pre"; # LINT.ThenChange(main.go:version) in { + # Formatter to be used by helped func + # Nix package packages = forAllSystems (system: let pkgs = pkgsFor system; in { @@ -33,7 +38,7 @@ devShells = forAllSystems (system: let pkgs = pkgsFor system; in { default = pkgs.mkShell { - buildInputs = [ pkgs.go pkgs.gopls pkgs.nixfmt-rfc-style ]; + buildInputs = [ pkgs.go pkgs.gopls pkgs.nixfmt-rfc-style alejandra.defaultPackage.${system} ]; # Add a Git pre-commit hook. shellHook = onchg.shellHook.${system}; }; diff --git a/helpers.go b/helpers.go index b4b7a6d..0b0564e 100644 --- a/helpers.go +++ b/helpers.go @@ -76,14 +76,15 @@ func ReadEnvFiles(envFiles []string, mergeWithEnv, ignoreMissing bool) (env []st // formatNixCode will format Nix code by calling 'nixfmt' and passing in the // given code via stdin. -func formatNixCode(contents []byte) ([]byte, error) { +func formatNixCode(contents []byte, formatter string) ([]byte, error) { + // // Check for existence of 'nixfmt' in $PATH. - nixfmtPath, err := exec.LookPath("nixfmt") + formatterPath, err := exec.LookPath(formatter) if err != nil { - return nil, fmt.Errorf("'nixfmt' not found in $PATH: %w", err) + return nil, fmt.Errorf("%w not found in $PATH: %w", formatter, err) } - cmd := exec.Command(nixfmtPath) + cmd := exec.Command(formatterPath) cmd.Stdin = bytes.NewBuffer(contents) // Overwrite contents with formatted output. diff --git a/main.go b/main.go index 9aeac6b..58b0d1c 100644 --- a/main.go +++ b/main.go @@ -40,7 +40,7 @@ var createRootTarget = flag.Bool("create_root_target", true, "if set, a root sys var defaultStopTimeout = flag.Duration("default_stop_timeout", defaultSystemdStopTimeout, "default stop timeout for generated container services.") var build = flag.Bool("build", false, "if set, generated container build systemd services will be enabled.") var writeNixSetup = flag.Bool("write_nix_setup", true, "if true, Nix setup code is written to output (runtime, DNS, autoprune, etc.)") -var autoFormat = flag.Bool("auto_format", false, `if true, Nix output will be formatted using "nixfmt" (must be present in $PATH).`) +var formatter = flag.String("formatter", "", `if specified, Nix output will be formatted by formatter specified, supported are nixfmt and alejandra (must be present in $PATH).`) var version = flag.Bool("version", false, "display version and exit") type OsGetWd struct{} @@ -51,7 +51,7 @@ func (*OsGetWd) GetWd() (string, error) { func main() { flag.Parse() - + fmt.Println(*formatter) if *version { fmt.Printf("compose2nix v%s\n", appVersion) return @@ -90,6 +90,9 @@ func main() { } serviceIncludeRegexp = pat } + if *formatter != "" && (strings.TrimSpace(*formatter) != "nixfmt" && strings.TrimSpace(*formatter) != "alejandra") { + log.Fatal("Invalid formatter provided, needs to be either nixfmt or alejandra") + } start := time.Now() g := Generator{ @@ -112,7 +115,8 @@ func main() { NoCreateRootTarget: !*createRootTarget, WriteHeader: true, NoWriteNixSetup: !*writeNixSetup, - AutoFormat: *autoFormat, + AutoFormat: *formatter != "", + Formatter: *formatter, DefaultStopTimeout: *defaultStopTimeout, IncludeBuild: *build, GetWorkingDir: &OsGetWd{}, diff --git a/nix.go b/nix.go index 6e7942c..7a9e57b 100644 --- a/nix.go +++ b/nix.go @@ -281,6 +281,7 @@ type NixContainerConfig struct { Volumes []*NixVolume CreateRootTarget bool WriteNixSetup bool + Formatter string AutoFormat bool AutoStart bool IncludeBuild bool @@ -310,7 +311,7 @@ func (c *NixContainerConfig) Write(out io.Writer) error { config := []byte(c.String()) if c.AutoFormat { - formatted, err := formatNixCode(config) + formatted, err := formatNixCode(config, c.Formatter) if err != nil { return err }