terraform-provider-nix
terraform-provider-nix copied to clipboard
Add import support
I imagine it would be terraform import nix_nixos.foo <readlink /run/current-system on remote host>.
not sure it is so easy. I can't think of a way to find the configuration of a machine, especially if it is split into multiple modules. Maybe the cases of a single configuration.nix in the default location could work.
Oh, would you need to do that? I thought it would be enough to just store the store path that the current-system symlink points to
I don't think I fully understand the use case. If you write a new resource pointing to an existing nixos install, it will read the system and update only if it differs already - It won't really do anything destructive.
It's just about bringing an existing network into Terraform. For example, I have a machine x that's running some NixOS configuration. I'd like to be able to import that into my Terraform state, such that the initial terraform plan says there is nothing to do. It's harmless to deploy the current running system, but it is redundant and makes it difficult to make sure my Terraform configuration matches what I'm currently running. To clarify, this is about the initial work of moving existing infrastructure into Terraform.
Perhaps another way to think about it is: I should be able to write a Terraform configuration, deploying it, delete my state, and then using only terraform import I should be able to reach a state where terraform plan says there is nothing to do. Or maybe you are saying that should already be the case?
Hmm, terraform plan may say it is creating a new resource - but then when the creation actually runs, I don't think anything would happen. I will leave this issue open though, maybe it isn't too hard to support.
I don't know if you're interested in this anymore.
Usually, resources are based on some canonical ID, like an ARN in AWS; and that state can be used to find the resource in the real world and load its state the next time Read is invoked.
Here however, the resources are given random IDs, as finding the nixos server is simply a matter of interrogating the host via ssh. So I think for a terraform import the best thing to do would be to ignore the final argument and just assign a random ID as well.
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
d.SetId(randomID())
return []*schema.ResourceData{d}, nil
},
},
Then when Read is invoked on the next plan, it will get the current state of the operating system and not say it has to create it (or update it at all if it is indeed the right system state).