From edefef4e1adbf3721175023c23424a64ac6fb7d9 Mon Sep 17 00:00:00 2001 From: bjk Date: Wed, 20 Sep 2023 17:56:40 +0000 Subject: [PATCH 1/6] feat: use flakehub for installs, self-manage fleek, autogc --- internal/flake/flake.go | 15 ++++++++ internal/flake/templates/flake.nix.tmpl | 13 +++++-- internal/fleek/config.go | 14 ++++++++ internal/fleekcli/root.go | 11 +++++- internal/fleekcli/update.go | 3 ++ internal/fleekcli/version.go | 12 ------- internal/fleekcli/write.go | 48 +++++++++++++++++++++++++ locales/en.yml | 7 ++++ 8 files changed, 107 insertions(+), 16 deletions(-) create mode 100644 internal/fleekcli/write.go diff --git a/internal/flake/flake.go b/internal/flake/flake.go index 9d2494ff..9d8d26c4 100644 --- a/internal/flake/flake.go +++ b/internal/flake/flake.go @@ -537,6 +537,21 @@ func (f *Flake) writeUser(sys fleek.System, user fleek.User, template string, fo return nil } + +func (f *Flake) WriteTemplates() error { + + writeCmdLine := []string{"run", ".#fleek" , "--", "write"} + err := f.runNix(nixbin, writeCmdLine) + if err != nil { + return err + } + err = f.mayCommit("fleek: update templates") + + if err != nil { + return err + } + return nil +} func (f *Flake) Apply() error { fin.Info.Println(f.app.Trans("flake.apply")) diff --git a/internal/flake/templates/flake.nix.tmpl b/internal/flake/templates/flake.nix.tmpl index 6b608671..8c224869 100644 --- a/internal/flake/templates/flake.nix.tmpl +++ b/internal/flake/templates/flake.nix.tmpl @@ -7,11 +7,11 @@ nixpkgs.url = "github:nixos/nixpkgs/{{ .Config.Tracks }}"; # Home manager - home-manager.url = "github:nix-community/home-manager"; + home-manager.url = "https://flakehub.com/f/nix-community/home-manager/0.1.tar.gz"; home-manager.inputs.nixpkgs.follows = "nixpkgs"; # Fleek - fleek.url = "github:ublue-os/fleek"; + fleek.url = "https://flakehub.com/f/ublue-os/fleek/*.tar.gz"; # Overlays {{ range $index, $element := .Config.Overlays }} @@ -22,7 +22,9 @@ }; outputs = { self, nixpkgs, home-manager, fleek, ... }@inputs: { - + {{ range .Config.Systems }} + packages.{{ .Arch }}-{{ .OS }}.fleek = fleek.packages.{{ .Arch }}-{{ .OS }}.default; + {{ end }} # Available through 'home-manager --flake .#your-username@your-hostname' {{ $overlays := .Config.Overlays }} homeConfigurations = { @@ -41,6 +43,11 @@ ./{{.Hostname}}/{{.Username}}.nix ./{{.Hostname}}/custom.nix # self-manage fleek + { + home.packages = [ + fleek.packages.{{ .Arch }}-{{ .OS }}.default + ]; + } ({ nixpkgs.overlays = [{{ range $index, $element := $overlays }}inputs.{{$index}}.overlay {{ end }}]; }) diff --git a/internal/fleek/config.go b/internal/fleek/config.go index d6b07248..b3d86b73 100644 --- a/internal/fleek/config.go +++ b/internal/fleek/config.go @@ -59,6 +59,7 @@ type Config struct { Users []*User `yaml:",flow"` Track string `yaml:"track"` AllowBroken bool `yaml:"allow_broken"` + AutoGC bool `yaml:"auto_gc"` } func Levels() []string { @@ -120,6 +121,18 @@ func NewSystem() (*System, error) { Username: user, }, nil } + +func CollectGarbage() error { + + command := exec.Command("nix-collect-garbage", "-d") + command.Stdin = os.Stdin + command.Stderr = os.Stderr + command.Stdout = os.Stdout + command.Env = os.Environ() + + return command.Run() + +} func NewUser() (*User, error) { fin.Info.Println("Enter User Details for Git Configuration:") user := &User{} @@ -460,6 +473,7 @@ func (c *Config) WriteInitialConfig(force bool, symlink bool) error { return err } c.Unfree = true + c.AutoGC = true c.Name = "Fleek Configuration" c.Packages = []string{ "helix", diff --git a/internal/fleekcli/root.go b/internal/fleekcli/root.go index 05d874ab..c50859c4 100644 --- a/internal/fleekcli/root.go +++ b/internal/fleekcli/root.go @@ -101,6 +101,13 @@ func RootCmd() *cobra.Command { fin.Debug.Printfln("git autopush: %v", cfg.Git.AutoPush) fin.Debug.Printfln("git autocommit: %v", cfg.Git.AutoCommit) fin.Debug.Printfln("git autopull: %v", cfg.Git.AutoPull) + fin.Debug.Printfln("auto gc: %v", cfg.AutoGC) + + if cfg.AutoGC { + fin.Info.Println("Running nix-collect-garbage") + // we don't care too much if there's an error here + fleek.CollectGarbage() + } }, RunE: func(cmd *cobra.Command, args []string) error { @@ -152,6 +159,8 @@ func RootCmd() *cobra.Command { infoCmd := InfoCommand() infoCmd.GroupID = packageGroup.ID + writeCmd := WriteCommand() + writeCmd.GroupID = fleekGroup.ID manCmd := ManCommand() docsCmd := genDocsCmd() @@ -171,7 +180,7 @@ func RootCmd() *cobra.Command { command.AddCommand(searchCmd) command.AddCommand(infoCmd) command.AddCommand(generateCmd) - + command.AddCommand(writeCmd) command.AddCommand(VersionCmd()) command.PersistentFlags().BoolVarP( diff --git a/internal/fleekcli/update.go b/internal/fleekcli/update.go index 29e41c86..b462da28 100644 --- a/internal/fleekcli/update.go +++ b/internal/fleekcli/update.go @@ -48,6 +48,9 @@ func update(cmd *cobra.Command) error { if err := fl.Update(); err != nil { return err } + if err := fl.WriteTemplates(); err != nil { + return err + } if cmd.Flag(app.Trans("update.applyFlag")).Changed { if err := fl.Apply(); err != nil { return err diff --git a/internal/fleekcli/version.go b/internal/fleekcli/version.go index cc6626c1..c209c6f0 100644 --- a/internal/fleekcli/version.go +++ b/internal/fleekcli/version.go @@ -27,7 +27,6 @@ func VersionCmd() *cobra.Command { command.Flags().BoolVarP(&flags.verbose, app.Trans("version.flagVerbose"), "v", false, // value app.Trans("version.flagVerboseDescription"), ) - command.AddCommand(selfUpdateCmd()) return command } @@ -47,18 +46,7 @@ func versionCmdFunc(cmd *cobra.Command, _ []string, flags versionFlags) error { } return nil } -func selfUpdateCmd() *cobra.Command { - command := &cobra.Command{ - Use: "update", - Short: "Update fleek launcher and binary", - Args: cobra.ExactArgs(0), - RunE: func(cmd *cobra.Command, args []string) error { - return vercheck.SelfUpdate(cmd.OutOrStdout(), cmd.OutOrStderr()) - }, - } - return command -} type versionInfo struct { Version string diff --git a/internal/fleekcli/write.go b/internal/fleekcli/write.go new file mode 100644 index 00000000..c9a759f4 --- /dev/null +++ b/internal/fleekcli/write.go @@ -0,0 +1,48 @@ +/* +Copyright © 2023 NAME HERE +*/ +package fleekcli + +import ( + "github.com/spf13/cobra" + "github.com/ublue-os/fleek/fin" + "github.com/ublue-os/fleek/internal/flake" +) + + + +func WriteCommand() *cobra.Command { + command := &cobra.Command{ + Hidden: true, + Use: app.Trans("write.use"), + Short: app.Trans("write.short"), + Long: app.Trans("write.long"), + RunE: func(cmd *cobra.Command, args []string) error { + return write(cmd) + }, + } + + return command +} + +// writeCmd represents the write command +func write(cmd *cobra.Command) error { + fin.Description.Println(cmd.Short) + err := mustConfig() + if err != nil { + return err + } + fl, err := flake.Load(cfg, app) + if err != nil { + return err + } + + err = fl.Write("flake update", true, false) + if err != nil { + fin.Debug.Printfln("flake write error: %s", err) + return err + } + + fin.Success.Println(app.Trans("write.done")) + return nil +} diff --git a/locales/en.yml b/locales/en.yml index 564bae79..c6be01d2 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -238,6 +238,13 @@ info: notFound: "That program or package is not part of fleek's bling set." aliases: "Shell Aliases" description: "Description" +write: + use: "write" + long: "Apply system templates to existing flake" + example: | + fleek write + short: "Apply system templates to existing flake" + done: "Flake templates written." flake: noConfig: "No configuration files found. Try `fleek init`." configLoaded: "Configuration loaded" From be3d646d52ba45c0fa0bbb2329fcbdbc15d2b109 Mon Sep 17 00:00:00 2001 From: bjk Date: Wed, 20 Sep 2023 17:59:45 +0000 Subject: [PATCH 2/6] fix: doc comments --- internal/fleek/config.go | 1 + internal/fleekcli/update.go | 3 +++ internal/fleekcli/write.go | 3 ++- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/fleek/config.go b/internal/fleek/config.go index b3d86b73..2be51905 100644 --- a/internal/fleek/config.go +++ b/internal/fleek/config.go @@ -122,6 +122,7 @@ func NewSystem() (*System, error) { }, nil } +// CollectGarbage runs nix-collect-garbage func CollectGarbage() error { command := exec.Command("nix-collect-garbage", "-d") diff --git a/internal/fleekcli/update.go b/internal/fleekcli/update.go index b462da28..778ea4b7 100644 --- a/internal/fleekcli/update.go +++ b/internal/fleekcli/update.go @@ -48,6 +48,9 @@ func update(cmd *cobra.Command) error { if err := fl.Update(); err != nil { return err } + // We just updated the flake lock, which might pull a new + // version of fleek in. Update the system templates to + // get new fixes without having to update/apply twice if err := fl.WriteTemplates(); err != nil { return err } diff --git a/internal/fleekcli/write.go b/internal/fleekcli/write.go index c9a759f4..44236e35 100644 --- a/internal/fleekcli/write.go +++ b/internal/fleekcli/write.go @@ -10,7 +10,8 @@ import ( ) - +// WriteCommand is an internal hidden command that +// gets run after an update. func WriteCommand() *cobra.Command { command := &cobra.Command{ Hidden: true, From 27f5990d49a1f858a91afe5d3c45c2b64c7e2991 Mon Sep 17 00:00:00 2001 From: bjk Date: Wed, 20 Sep 2023 18:01:03 +0000 Subject: [PATCH 3/6] fix: fmt --- internal/flake/flake.go | 2 +- internal/fleek/config.go | 2 +- internal/fleekcli/update.go | 2 +- internal/fleekcli/version.go | 1 - internal/fleekcli/write.go | 9 ++++----- 5 files changed, 7 insertions(+), 9 deletions(-) diff --git a/internal/flake/flake.go b/internal/flake/flake.go index 9d8d26c4..a12e594e 100644 --- a/internal/flake/flake.go +++ b/internal/flake/flake.go @@ -540,7 +540,7 @@ func (f *Flake) writeUser(sys fleek.System, user fleek.User, template string, fo func (f *Flake) WriteTemplates() error { - writeCmdLine := []string{"run", ".#fleek" , "--", "write"} + writeCmdLine := []string{"run", ".#fleek", "--", "write"} err := f.runNix(nixbin, writeCmdLine) if err != nil { return err diff --git a/internal/fleek/config.go b/internal/fleek/config.go index 2be51905..0aafc011 100644 --- a/internal/fleek/config.go +++ b/internal/fleek/config.go @@ -59,7 +59,7 @@ type Config struct { Users []*User `yaml:",flow"` Track string `yaml:"track"` AllowBroken bool `yaml:"allow_broken"` - AutoGC bool `yaml:"auto_gc"` + AutoGC bool `yaml:"auto_gc"` } func Levels() []string { diff --git a/internal/fleekcli/update.go b/internal/fleekcli/update.go index 778ea4b7..bc5331dd 100644 --- a/internal/fleekcli/update.go +++ b/internal/fleekcli/update.go @@ -49,7 +49,7 @@ func update(cmd *cobra.Command) error { return err } // We just updated the flake lock, which might pull a new - // version of fleek in. Update the system templates to + // version of fleek in. Update the system templates to // get new fixes without having to update/apply twice if err := fl.WriteTemplates(); err != nil { return err diff --git a/internal/fleekcli/version.go b/internal/fleekcli/version.go index c209c6f0..328ebeae 100644 --- a/internal/fleekcli/version.go +++ b/internal/fleekcli/version.go @@ -47,7 +47,6 @@ func versionCmdFunc(cmd *cobra.Command, _ []string, flags versionFlags) error { return nil } - type versionInfo struct { Version string IsPrerelease bool diff --git a/internal/fleekcli/write.go b/internal/fleekcli/write.go index 44236e35..d455a0d1 100644 --- a/internal/fleekcli/write.go +++ b/internal/fleekcli/write.go @@ -9,15 +9,14 @@ import ( "github.com/ublue-os/fleek/internal/flake" ) - // WriteCommand is an internal hidden command that // gets run after an update. func WriteCommand() *cobra.Command { command := &cobra.Command{ Hidden: true, - Use: app.Trans("write.use"), - Short: app.Trans("write.short"), - Long: app.Trans("write.long"), + Use: app.Trans("write.use"), + Short: app.Trans("write.short"), + Long: app.Trans("write.long"), RunE: func(cmd *cobra.Command, args []string) error { return write(cmd) }, @@ -37,7 +36,7 @@ func write(cmd *cobra.Command) error { if err != nil { return err } - + err = fl.Write("flake update", true, false) if err != nil { fin.Debug.Printfln("flake write error: %s", err) From 0d6b216ef72bc2630771b03b979450f39d3308bd Mon Sep 17 00:00:00 2001 From: bjk Date: Wed, 20 Sep 2023 18:02:40 +0000 Subject: [PATCH 4/6] fix: fmt --- internal/fleekcli/version.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/fleekcli/version.go b/internal/fleekcli/version.go index 328ebeae..e8eb0927 100644 --- a/internal/fleekcli/version.go +++ b/internal/fleekcli/version.go @@ -6,7 +6,6 @@ import ( "github.com/spf13/cobra" "github.com/ublue-os/fleek/internal/build" - "github.com/ublue-os/fleek/internal/vercheck" ) type versionFlags struct { From 3992faf7b30f2fab0df975e1c69d954112370fa0 Mon Sep 17 00:00:00 2001 From: bjk Date: Wed, 20 Sep 2023 18:03:52 +0000 Subject: [PATCH 5/6] fix: discard error --- internal/fleekcli/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/fleekcli/root.go b/internal/fleekcli/root.go index c50859c4..6063a6ac 100644 --- a/internal/fleekcli/root.go +++ b/internal/fleekcli/root.go @@ -106,7 +106,7 @@ func RootCmd() *cobra.Command { if cfg.AutoGC { fin.Info.Println("Running nix-collect-garbage") // we don't care too much if there's an error here - fleek.CollectGarbage() + _ = fleek.CollectGarbage() } }, From a139cebd188815bc83ca26a57fdd6ee951bc92b4 Mon Sep 17 00:00:00 2001 From: bjk Date: Wed, 20 Sep 2023 18:36:13 +0000 Subject: [PATCH 6/6] fix: readme --- README.md | 47 +++-------------------------------------------- 1 file changed, 3 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index a192c0a5..f7ef2b40 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Fleek - "Home as Code" for Humans -![FlakeHub](https://img.shields.io/endpoint?url=https://flakehub.com/f/ublue-os/fleek/badge) + +[![FlakeHub](https://img.shields.io/endpoint?url=https://flakehub.com/f/ublue-os/fleek/badge)](https://flakehub.com/flake/ublue-os/fleek) @@ -44,49 +45,7 @@ Fleek is a user-friendly wrapper around Nix and Nix Home Manager, but the friend ## Getting Started -You need `nix`. We love the [Determinate Systems Installer](https://zero-to-nix.com/), but any `nix` installer is good. If you're on Fedora Silverblue, [this script](https://github.com/dnkmmr69420/nix-installer-scripts/blob/main/installer-scripts/silverblue-nix-installer.sh) is the good stuff. - -After Nix is installed you need to enable [flakes and the nix command](https://nixos.wiki/wiki/Flakes). It can be as simple as this: - -```shell -mkdir -p ~/.config/nix -echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf -``` - -Install `fleek`: - -```shell -curl -fsSL https://getfleek.dev/installer | bash -``` - -Run `fleek init`: - -```shell -fleek init -``` - - -This will create your configuration file and symlink it to `$HOME/.fleek.yml`. Open it with your favorite editor and take a look. - -Make any changes to the `~/.fleek.yml` file you want... we recommend Bling Level `high` for the best experience. - -Now let's apply your configuration: - -```shell -fleek apply -``` -It will take a bit to download and install everything, but when it's done you should see something like this: - -```shell -... more text above this ... -Activating onFilesChange -Activating reloadSystemd - [✓] Operation completed successfully -``` - -*What happened here?* We just installed Nix Home Manager, configured it with your preferences, and applied it to your system. - -You may need to close and re-open your terminal or even log out to see the changes. +See [the installation instructions](https://getfleek.dev/docs/installation). ## ~/.fleek.yml